Skip to content

Commit 6744d84

Browse files
committed
Merge bitcoin/bitcoin#27745: addrman: select addresses by network follow-up
cd8ef5b test: ensure addrman test is finite (Amiti Uttarwar) b9f1e86 addrman: change asserts to Assumes (Amiti Uttarwar) 7687707 doc: update `Select` function description (Amiti Uttarwar) 2b6bd12 refactor: de-duplicate lookups (Amiti Uttarwar) Pull request description: this PR addresses outstanding review comments from #27214 ACKs for top commit: achow101: ACK cd8ef5b mzumsande: Code Review ACK cd8ef5b brunoerg: crACK cd8ef5b Tree-SHA512: 669f67904263e3f51c39b175eabf5fa1b1e7b6841e889656afec33d0bd93fb446de9403f0a91b186ddeaf29498c8938484a0547b1188256c4e7c90db6f30bb55
2 parents a8bd0fe + cd8ef5b commit 6744d84

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

src/addrman.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -757,16 +757,14 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, std::option
757757

758758
// Iterate over the positions of that bucket, starting at the initial one,
759759
// and looping around.
760-
int i;
760+
int i, position, node_id;
761761
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
762-
int position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
763-
int node_id = GetEntry(search_tried, bucket, position);
762+
position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
763+
node_id = GetEntry(search_tried, bucket, position);
764764
if (node_id != -1) {
765765
if (network.has_value()) {
766766
const auto it{mapInfo.find(node_id)};
767-
assert(it != mapInfo.end());
768-
const auto info{it->second};
769-
if (info.GetNetwork() == *network) break;
767+
if (Assume(it != mapInfo.end()) && it->second.GetNetwork() == *network) break;
770768
} else {
771769
break;
772770
}
@@ -777,9 +775,7 @@ std::pair<CAddress, NodeSeconds> AddrManImpl::Select_(bool new_only, std::option
777775
if (i == ADDRMAN_BUCKET_SIZE) continue;
778776

779777
// Find the entry to return.
780-
int position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
781-
int nId = GetEntry(search_tried, bucket, position);
782-
const auto it_found{mapInfo.find(nId)};
778+
const auto it_found{mapInfo.find(node_id)};
783779
assert(it_found != mapInfo.end());
784780
const AddrInfo& info{it_found->second};
785781

@@ -798,15 +794,17 @@ int AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
798794
{
799795
AssertLockHeld(cs);
800796

801-
assert(position < ADDRMAN_BUCKET_SIZE);
802-
803797
if (use_tried) {
804-
assert(bucket < ADDRMAN_TRIED_BUCKET_COUNT);
805-
return vvTried[bucket][position];
798+
if (Assume(position < ADDRMAN_BUCKET_SIZE) && Assume(bucket < ADDRMAN_TRIED_BUCKET_COUNT)) {
799+
return vvTried[bucket][position];
800+
}
806801
} else {
807-
assert(bucket < ADDRMAN_NEW_BUCKET_COUNT);
808-
return vvNew[bucket][position];
802+
if (Assume(position < ADDRMAN_BUCKET_SIZE) && Assume(bucket < ADDRMAN_NEW_BUCKET_COUNT)) {
803+
return vvNew[bucket][position];
804+
}
809805
}
806+
807+
return -1;
810808
}
811809

812810
std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network) const

src/addrman.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,10 @@ class AddrMan
148148
*
149149
* @param[in] new_only Whether to only select addresses from the new table. Passing `true` returns
150150
* an address from the new table or an empty pair. Passing `false` will return an
151-
* address from either the new or tried table (it does not guarantee a tried entry).
152-
* @param[in] network Select only addresses of this network (nullopt = all)
151+
* empty pair or an address from either the new or tried table (it does not
152+
* guarantee a tried entry).
153+
* @param[in] network Select only addresses of this network (nullopt = all). Passing a network may
154+
* slow down the search.
153155
* @return CAddress The record for the selected peer.
154156
* seconds The last time we attempted to connect to that peer.
155157
*/

src/addrman_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class AddrManImpl
255255

256256
/** Helper to generalize looking up an addrman entry from either table.
257257
*
258-
* @return int The nid of the entry or -1 if the addrman position is empty.
258+
* @return int The nid of the entry. If the addrman position is empty or not found, returns -1.
259259
* */
260260
int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
261261

src/test/addrman_tests.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,9 @@ BOOST_AUTO_TEST_CASE(addrman_select_by_network)
239239
// ensure that both new and tried table are selected from
240240
bool new_selected{false};
241241
bool tried_selected{false};
242+
int counter = 256;
242243

243-
while (!new_selected || !tried_selected) {
244+
while (--counter > 0 && (!new_selected || !tried_selected)) {
244245
const CAddress selected{addrman->Select(/*new_only=*/false, NET_I2P).first};
245246
BOOST_REQUIRE(selected == i2p_addr || selected == i2p_addr2);
246247
if (selected == i2p_addr) {
@@ -249,6 +250,9 @@ BOOST_AUTO_TEST_CASE(addrman_select_by_network)
249250
new_selected = true;
250251
}
251252
}
253+
254+
BOOST_CHECK(new_selected);
255+
BOOST_CHECK(tried_selected);
252256
}
253257

254258
BOOST_AUTO_TEST_CASE(addrman_select_special)

0 commit comments

Comments
 (0)