Skip to content

Commit 7d149c9

Browse files
committed
merge bitcoin#27745: select addresses by network follow-up
1 parent 1d82994 commit 7d149c9

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
@@ -750,16 +750,14 @@ std::pair<CAddress, int64_t> AddrManImpl::Select_(bool new_only, std::optional<N
750750

751751
// Iterate over the positions of that bucket, starting at the initial one,
752752
// and looping around.
753-
int i;
753+
int i, position, node_id;
754754
for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
755-
int position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
756-
int node_id = GetEntry(search_tried, bucket, position);
755+
position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
756+
node_id = GetEntry(search_tried, bucket, position);
757757
if (node_id != -1) {
758758
if (network.has_value()) {
759759
const auto it{mapInfo.find(node_id)};
760-
assert(it != mapInfo.end());
761-
const auto info{it->second};
762-
if (info.GetNetwork() == *network) break;
760+
if (Assume(it != mapInfo.end()) && it->second.GetNetwork() == *network) break;
763761
} else {
764762
break;
765763
}
@@ -770,9 +768,7 @@ std::pair<CAddress, int64_t> AddrManImpl::Select_(bool new_only, std::optional<N
770768
if (i == ADDRMAN_BUCKET_SIZE) continue;
771769

772770
// Find the entry to return.
773-
int position = (initial_position + i) % ADDRMAN_BUCKET_SIZE;
774-
int nId = GetEntry(search_tried, bucket, position);
775-
const auto it_found{mapInfo.find(nId)};
771+
const auto it_found{mapInfo.find(node_id)};
776772
assert(it_found != mapInfo.end());
777773
const AddrInfo& info{it_found->second};
778774

@@ -791,15 +787,17 @@ int AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const
791787
{
792788
AssertLockHeld(cs);
793789

794-
assert(position < ADDRMAN_BUCKET_SIZE);
795-
796790
if (use_tried) {
797-
assert(bucket < ADDRMAN_TRIED_BUCKET_COUNT);
798-
return vvTried[bucket][position];
791+
if (Assume(position < ADDRMAN_BUCKET_SIZE) && Assume(bucket < ADDRMAN_TRIED_BUCKET_COUNT)) {
792+
return vvTried[bucket][position];
793+
}
799794
} else {
800-
assert(bucket < ADDRMAN_NEW_BUCKET_COUNT);
801-
return vvNew[bucket][position];
795+
if (Assume(position < ADDRMAN_BUCKET_SIZE) && Assume(bucket < ADDRMAN_NEW_BUCKET_COUNT)) {
796+
return vvNew[bucket][position];
797+
}
802798
}
799+
800+
return -1;
803801
}
804802

805803
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
@@ -159,8 +159,10 @@ class AddrMan
159159
*
160160
* @param[in] new_only Whether to only select addresses from the new table. Passing `true` returns
161161
* an address from the new table or an empty pair. Passing `false` will return an
162-
* address from either the new or tried table (it does not guarantee a tried entry).
163-
* @param[in] network Select only addresses of this network (nullopt = all)
162+
* empty pair or an address from either the new or tried table (it does not
163+
* guarantee a tried entry).
164+
* @param[in] network Select only addresses of this network (nullopt = all). Passing a network may
165+
* slow down the search.
164166
* @return CAddress The record for the selected peer.
165167
* int64_t The last time we attempted to connect to that peer.
166168
*/

src/addrman_impl.h

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

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

src/test/addrman_tests.cpp

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

241-
while (!new_selected || !tried_selected) {
242+
while (--counter > 0 && (!new_selected || !tried_selected)) {
242243
const CAddress selected{addrman->Select(/*new_only=*/false, NET_I2P).first};
243244
BOOST_REQUIRE(selected == i2p_addr || selected == i2p_addr2);
244245
if (selected == i2p_addr) {
@@ -247,6 +248,9 @@ BOOST_AUTO_TEST_CASE(addrman_select_by_network)
247248
new_selected = true;
248249
}
249250
}
251+
252+
BOOST_CHECK(new_selected);
253+
BOOST_CHECK(tried_selected);
250254
}
251255

252256
BOOST_AUTO_TEST_CASE(addrman_select_special)

0 commit comments

Comments
 (0)