Skip to content

Commit 4977073

Browse files
kwvgPastaPastaPasta
authored andcommitted
merge bitcoin#22497: remove ResetI2PPorts()
1 parent 63d5853 commit 4977073

File tree

3 files changed

+0
-225
lines changed

3 files changed

+0
-225
lines changed

src/addrman.cpp

Lines changed: 0 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <addrman.h>
77

88
#include <hash.h>
9-
#include <i2p.h>
109
#include <logging.h>
1110
#include <netaddress.h>
1211
#include <serialize.h>
@@ -778,100 +777,3 @@ std::vector<bool> CAddrMan::DecodeAsmap(fs::path path)
778777
}
779778
return bits;
780779
}
781-
782-
void CAddrMan::ResetI2PPorts()
783-
{
784-
for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; ++bucket) {
785-
for (int i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
786-
const auto id = vvNew[bucket][i];
787-
if (id == -1) {
788-
continue;
789-
}
790-
auto it = mapInfo.find(id);
791-
if (it == mapInfo.end()) {
792-
return;
793-
}
794-
auto& addr_info = it->second;
795-
if (!addr_info.IsI2P() || addr_info.GetPort() == I2P_SAM31_PORT) {
796-
continue;
797-
}
798-
799-
auto addr_info_newport = addr_info;
800-
// The below changes addr_info_newport.GetKey(), which is used in finding a
801-
// bucket and a position within that bucket. So a re-bucketing may be necessary.
802-
addr_info_newport.port = I2P_SAM31_PORT;
803-
804-
// Reposition entries of vvNew within the same bucket because we don't know the source
805-
// address which led to the decision to store the entry in vvNew[bucket] so we can't
806-
// re-evaluate that decision, but even if we could, CAddrInfo::GetNewBucket() does not
807-
// use CAddrInfo::GetKey() so it would end up in the same bucket as before the port
808-
// change.
809-
const auto i_target = addr_info_newport.GetBucketPosition(nKey, true, bucket);
810-
811-
if (i_target == i) { // No need to re-position.
812-
addr_info = addr_info_newport;
813-
continue;
814-
}
815-
816-
// Reposition from i to i_target, removing the entry from i_target (if any).
817-
ClearNew(bucket, i_target);
818-
vvNew[bucket][i_target] = id;
819-
vvNew[bucket][i] = -1;
820-
addr_info = addr_info_newport;
821-
}
822-
}
823-
824-
for (int bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; ++bucket) {
825-
for (int i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
826-
const auto id = vvTried[bucket][i];
827-
if (id == -1) {
828-
continue;
829-
}
830-
auto it = mapInfo.find(id);
831-
if (it == mapInfo.end()) {
832-
return;
833-
}
834-
auto& addr_info = it->second;
835-
if (!addr_info.IsI2P() || addr_info.GetPort() == I2P_SAM31_PORT) {
836-
continue;
837-
}
838-
839-
auto addr_info_newport = addr_info;
840-
// The below changes addr_info_newport.GetKey(), which is used in finding a
841-
// bucket and a position within that bucket. So a re-bucketing may be necessary.
842-
addr_info_newport.port = I2P_SAM31_PORT;
843-
844-
const auto bucket_target = addr_info_newport.GetTriedBucket(nKey, m_asmap);
845-
const auto i_target = addr_info_newport.GetBucketPosition(nKey, false, bucket_target);
846-
847-
if (bucket_target == bucket && i_target == i) { // No need to re-position.
848-
addr_info = addr_info_newport;
849-
continue;
850-
}
851-
852-
// Reposition from (bucket, i) to (bucket_target, i_target). If the latter is
853-
// occupied, then move the entry from there to vvNew.
854-
855-
const auto old_target_id = vvTried[bucket_target][i_target];
856-
if (old_target_id != -1) {
857-
CAddrInfo& old_target_info = mapInfo[old_target_id];
858-
859-
old_target_info.fInTried = false;
860-
vvTried[bucket_target][i_target] = -1;
861-
--nTried;
862-
863-
const auto new_bucket = old_target_info.GetNewBucket(nKey, m_asmap);
864-
const auto new_bucket_i = old_target_info.GetBucketPosition(nKey, true, new_bucket);
865-
ClearNew(new_bucket, new_bucket_i);
866-
867-
old_target_info.nRefCount = 1;
868-
vvNew[new_bucket][new_bucket_i] = old_target_id;
869-
++nNew;
870-
}
871-
872-
vvTried[bucket_target][i_target] = id;
873-
vvTried[bucket][i] = -1;
874-
addr_info = addr_info_newport;
875-
}
876-
}
877-
}

src/addrman.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,6 @@ class CAddrMan
464464

465465
RemoveInvalid();
466466

467-
ResetI2PPorts();
468-
469467
Check();
470468
}
471469

@@ -800,14 +798,6 @@ class CAddrMan
800798
//! Remove invalid addresses.
801799
void RemoveInvalid() EXCLUSIVE_LOCKS_REQUIRED(cs);
802800

803-
/**
804-
* Reset the ports of I2P peers to 0.
805-
* This is needed as a temporary measure because now we enforce port 0 and
806-
* only connect to I2P hosts if the port is 0, but in the early days some
807-
* I2P addresses with port 8333 were rumoured and persisted into addrmans.
808-
*/
809-
void ResetI2PPorts() EXCLUSIVE_LOCKS_REQUIRED(cs);
810-
811801
friend class CAddrManTest;
812802
friend class CAddrManDeterministic;
813803
};

src/test/addrman_tests.cpp

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#include <addrman.h>
66
#include <hash.h>
7-
#include <i2p.h>
87
#include <netbase.h>
98
#include <random.h>
109
#include <test/data/asmap.raw.h>
@@ -968,121 +967,5 @@ BOOST_AUTO_TEST_CASE(addrman_evictionworks)
968967
BOOST_CHECK(addrman.SelectTriedCollision().ToString() == "[::]:0");
969968
}
970969

971-
BOOST_AUTO_TEST_CASE(reset_i2p_ports)
972-
{
973-
CAddrManTest addrman1;
974-
CAddrManTest addrman2;
975-
const uint32_t good_time{static_cast<uint32_t>(GetAdjustedTime())};
976-
constexpr uint16_t port = 8333;
977-
978-
// Has its port changed, will be re-positioned within the same bucket in vvNew.
979-
const CAddress i2p_new1{
980-
ResolveService("72l3ucjkuscrbiiepoehuwqgknyzgo7zuix5ty4puwrkyhtmnsga.b32.i2p", port),
981-
NODE_NONE,
982-
good_time};
983-
984-
// Has its port changed, will not be re-positioned in vvNew because ports 0 and 10075 result in
985-
// the same bucket position.
986-
const CAddress i2p_new2{
987-
ResolveService("gehtac45oaghz54ypyopim64mql7oad2bqclla74l6tfeolzmodq.b32.i2p", 10075),
988-
NODE_NONE,
989-
good_time};
990-
991-
// Remains unchanged, port is already as it should be.
992-
const CAddress i2p_new3{
993-
ResolveService("c4gfnttsuwqomiygupdqqqyy5y5emnk5c73hrfvatri67prd7vyq.b32.i2p",
994-
I2P_SAM31_PORT),
995-
NODE_NONE,
996-
good_time};
997-
998-
// Has its port changed, re-positioning in vvNew will cause i2p_new3 to be evicted.
999-
const CAddress i2p_new4{
1000-
ResolveService("c4cbbkn46qxftwja53pxiykntegfyfjqtnzbm6iv6r5mungmqgmq.b32.i2p", port),
1001-
NODE_NONE,
1002-
good_time};
1003-
1004-
// Remains unchanged.
1005-
const CAddress ipv4_new{ResolveService("1.2.3.4", port), NODE_NONE, good_time};
1006-
1007-
// Has its port changed, will be re-positioned in vvTried.
1008-
const CAddress i2p_tried1{
1009-
ResolveService("h3r6bkn46qxftwja53pxiykntegfyfjqtnzbm6iv6r5mungmqgmq.b32.i2p", port),
1010-
NODE_NONE,
1011-
good_time};
1012-
1013-
// Has its port changed, will not be re-positioned in vvTried because ports 0 and 10537
1014-
// result in the same position (bucket, i).
1015-
const CAddress i2p_tried2{
1016-
ResolveService("pjs7or2ctvteeo5tu4bwyrtydeuhqhvdprtujn4daxr75jpebjxa.b32.i2p", 10537),
1017-
NODE_NONE,
1018-
good_time};
1019-
1020-
// Remains unchanged, port is already as it should be.
1021-
const CAddress i2p_tried3{
1022-
ResolveService("hnbbyjpxx54623l555sta7pocy3se4sdgmuebi5k6reesz5rjp6q.b32.i2p",
1023-
I2P_SAM31_PORT),
1024-
NODE_NONE,
1025-
good_time};
1026-
1027-
// Has its port changed, re-positioning in vvTried will cause i2p_tried3 to be moved to vvNew.
1028-
const CAddress i2p_tried4{
1029-
ResolveService("hna37nqr3ahkqv62cuqfwgtneekvvpnuc4i4f6yo7tpoqjswvcwa.b32.i2p", port),
1030-
NODE_NONE,
1031-
good_time};
1032-
1033-
// Remains unchanged.
1034-
const CAddress ipv4_tried{ResolveService("2.3.4.5", port), NODE_NONE, good_time};
1035-
1036-
const CNetAddr source;
1037-
1038-
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
1039-
1040-
addrman1.Add(i2p_new1, source);
1041-
addrman1.Add(i2p_new2, source);
1042-
addrman1.Add(i2p_new3, source);
1043-
addrman1.Add(i2p_new4, source);
1044-
addrman1.Add(ipv4_new, source);
1045-
1046-
addrman1.Add(i2p_tried1, source);
1047-
addrman1.Good(i2p_tried1);
1048-
addrman1.Add(i2p_tried2, source);
1049-
addrman1.Good(i2p_tried2);
1050-
addrman1.Add(i2p_tried3, source);
1051-
addrman1.Good(i2p_tried3);
1052-
addrman1.Add(i2p_tried4, source);
1053-
addrman1.Good(i2p_tried4);
1054-
addrman1.Add(ipv4_tried, source);
1055-
addrman1.Good(ipv4_tried);
1056-
1057-
stream << addrman1;
1058-
stream >> addrman2;
1059-
1060-
const size_t max_addresses{0};
1061-
const size_t max_pct{0};
1062-
1063-
auto addresses = addrman2.GetAddr(max_addresses, max_pct, NET_I2P);
1064-
BOOST_REQUIRE_EQUAL(addresses.size(), 7UL);
1065-
std::sort(addresses.begin(), addresses.end()); // Just some deterministic order.
1066-
BOOST_CHECK_EQUAL(addresses[0].ToStringIP(), i2p_new4.ToStringIP());
1067-
BOOST_CHECK_EQUAL(addresses[0].GetPort(), I2P_SAM31_PORT);
1068-
BOOST_CHECK_EQUAL(addresses[1].ToStringIP(), i2p_new2.ToStringIP());
1069-
BOOST_CHECK_EQUAL(addresses[1].GetPort(), I2P_SAM31_PORT);
1070-
BOOST_CHECK_EQUAL(addresses[2].ToStringIP(), i2p_tried4.ToStringIP());
1071-
BOOST_CHECK_EQUAL(addresses[2].GetPort(), I2P_SAM31_PORT);
1072-
BOOST_CHECK_EQUAL(addresses[3].ToStringIP(), i2p_tried3.ToStringIP());
1073-
BOOST_CHECK_EQUAL(addresses[3].GetPort(), I2P_SAM31_PORT);
1074-
BOOST_CHECK_EQUAL(addresses[4].ToStringIP(), i2p_tried1.ToStringIP());
1075-
BOOST_CHECK_EQUAL(addresses[4].GetPort(), I2P_SAM31_PORT);
1076-
BOOST_CHECK_EQUAL(addresses[5].ToStringIP(), i2p_tried2.ToStringIP());
1077-
BOOST_CHECK_EQUAL(addresses[5].GetPort(), I2P_SAM31_PORT);
1078-
BOOST_CHECK_EQUAL(addresses[6].ToStringIP(), i2p_new1.ToStringIP());
1079-
BOOST_CHECK_EQUAL(addresses[6].GetPort(), I2P_SAM31_PORT);
1080-
1081-
addresses = addrman2.GetAddr(max_addresses, max_pct, NET_IPV4);
1082-
BOOST_REQUIRE_EQUAL(addresses.size(), 2UL);
1083-
std::sort(addresses.begin(), addresses.end()); // Just some deterministic order.
1084-
BOOST_CHECK_EQUAL(addresses[0].ToStringIPPort(), ipv4_new.ToStringIPPort());
1085-
BOOST_CHECK_EQUAL(addresses[1].ToStringIPPort(), ipv4_tried.ToStringIPPort());
1086-
}
1087970

1088971
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)