Skip to content

Commit fa27ee8

Browse files
author
MarcoFalke
committed
Get time less often in AddrManImpl::ResolveCollisions_()
This makes the code less verbose. Also, future changes that change how to get the time are less verbose. Moreover, GetAdjustedTime() might arbitrarily change the value during the execution of this function. For example, the system time advances over a second boundary, or the network adjusts the time arbitrarily. Most of the time however the value will not change, so it seems better to always lock the value in this scope for clarity.
1 parent 8c721ff commit fa27ee8

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/addrman.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -866,25 +866,27 @@ void AddrManImpl::ResolveCollisions_()
866866
int id_old = vvTried[tried_bucket][tried_bucket_pos];
867867
AddrInfo& info_old = mapInfo[id_old];
868868

869+
const auto current_time{GetAdjustedTime()};
870+
869871
// Has successfully connected in last X hours
870-
if (GetAdjustedTime() - info_old.nLastSuccess < ADDRMAN_REPLACEMENT_HOURS*(60*60)) {
872+
if (current_time - info_old.nLastSuccess < ADDRMAN_REPLACEMENT_HOURS*(60*60)) {
871873
erase_collision = true;
872-
} else if (GetAdjustedTime() - info_old.nLastTry < ADDRMAN_REPLACEMENT_HOURS*(60*60)) { // attempted to connect and failed in last X hours
874+
} else if (current_time - info_old.nLastTry < ADDRMAN_REPLACEMENT_HOURS*(60*60)) { // attempted to connect and failed in last X hours
873875

874876
// Give address at least 60 seconds to successfully connect
875-
if (GetAdjustedTime() - info_old.nLastTry > 60) {
877+
if (current_time - info_old.nLastTry > 60) {
876878
LogPrint(BCLog::ADDRMAN, "Replacing %s with %s in tried table\n", info_old.ToString(), info_new.ToString());
877879

878880
// Replaces an existing address already in the tried table with the new address
879-
Good_(info_new, false, GetAdjustedTime());
881+
Good_(info_new, false, current_time);
880882
erase_collision = true;
881883
}
882-
} else if (GetAdjustedTime() - info_new.nLastSuccess > ADDRMAN_TEST_WINDOW) {
884+
} else if (current_time - info_new.nLastSuccess > ADDRMAN_TEST_WINDOW) {
883885
// If the collision hasn't resolved in some reasonable amount of time,
884886
// just evict the old entry -- we must not be able to
885887
// connect to it for some reason.
886888
LogPrint(BCLog::ADDRMAN, "Unable to test; replacing %s with %s in tried table anyway\n", info_old.ToString(), info_new.ToString());
887-
Good_(info_new, false, GetAdjustedTime());
889+
Good_(info_new, false, current_time);
888890
erase_collision = true;
889891
}
890892
} else { // Collision is not actually a collision anymore

0 commit comments

Comments
 (0)