Skip to content

Commit 2c303cd

Browse files
codablockUdjinM6
authored andcommitted
A few devnet related fixes (#2168)
* Remove testnet seeds from devnet * Lift multiple ports restriction on devnet when considering new nodes Allow to connect to multiple nodes behind the same IP * Don't skip addresses with non-default port if it matches -port If the user specified -port, he very likely intends to connect to nodes with the same port. * Don't pass false to CAddrMan constructor as it is already the default * Make if statements easier to read
1 parent 050cabd commit 2c303cd

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

src/addrman.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,14 @@ double CAddrInfo::GetChance(int64_t nNow) const
6565
return fChance;
6666
}
6767

68-
CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int* pnId)
68+
CAddrInfo* CAddrMan::Find(const CService& addr, int* pnId)
6969
{
70-
std::map<CNetAddr, int>::iterator it = mapAddr.find(addr);
70+
CService addr2 = addr;
71+
if (!discriminatePorts) {
72+
addr2.SetPort(0);
73+
}
74+
75+
std::map<CService, int>::iterator it = mapAddr.find(addr2);
7176
if (it == mapAddr.end())
7277
return NULL;
7378
if (pnId)
@@ -80,9 +85,14 @@ CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int* pnId)
8085

8186
CAddrInfo* CAddrMan::Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId)
8287
{
88+
CService addr2 = addr;
89+
if (!discriminatePorts) {
90+
addr2.SetPort(0);
91+
}
92+
8393
int nId = nIdCount++;
8494
mapInfo[nId] = CAddrInfo(addr, addrSource);
85-
mapAddr[addr] = nId;
95+
mapAddr[addr2] = nId;
8696
mapInfo[nId].nRandomPos = vRandom.size();
8797
vRandom.push_back(nId);
8898
if (pnId)
@@ -117,9 +127,14 @@ void CAddrMan::Delete(int nId)
117127
assert(!info.fInTried);
118128
assert(info.nRefCount == 0);
119129

130+
CService addr = info;
131+
if (!discriminatePorts) {
132+
addr.SetPort(0);
133+
}
134+
120135
SwapRandom(info.nRandomPos, vRandom.size() - 1);
121136
vRandom.pop_back();
122-
mapAddr.erase(info);
137+
mapAddr.erase(addr);
123138
mapInfo.erase(nId);
124139
nNew--;
125140
}

src/addrman.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class CAddrMan
187187
std::map<int, CAddrInfo> mapInfo;
188188

189189
//! find an nId based on its network address
190-
std::map<CNetAddr, int> mapAddr;
190+
std::map<CService, int> mapAddr;
191191

192192
//! randomly-ordered vector of all nIds
193193
std::vector<int> vRandom;
@@ -207,6 +207,9 @@ class CAddrMan
207207
//! last time Good was called (memory only)
208208
int64_t nLastGood;
209209

210+
// discriminate entries based on port. Should be false on mainnet/testnet and can be true on devnet/regtest
211+
bool discriminatePorts;
212+
210213
protected:
211214
//! secret key to randomize bucket select with
212215
uint256 nKey;
@@ -215,7 +218,7 @@ class CAddrMan
215218
FastRandomContext insecure_rand;
216219

217220
//! Find an entry.
218-
CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
221+
CAddrInfo* Find(const CService& addr, int *pnId = NULL);
219222

220223
//! find an entry, creating it if necessary.
221224
//! nTime and nServices of the found node are updated, if necessary.
@@ -469,7 +472,8 @@ class CAddrMan
469472
nLastGood = 1; //Initially at 1 so that "never" is strictly worse.
470473
}
471474

472-
CAddrMan()
475+
CAddrMan(bool _discriminatePorts = false) :
476+
discriminatePorts(_discriminatePorts)
473477
{
474478
Clear();
475479
}

src/chainparams.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -366,8 +366,6 @@ class CTestNetParams : public CChainParams {
366366
// Testnet Dash BIP44 coin type is '1' (All coin's testnet default)
367367
nExtCoinType = 1;
368368

369-
vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test));
370-
371369
fMiningRequiresPeers = true;
372370
fDefaultConsistencyChecks = false;
373371
fRequireStandard = false;
@@ -503,7 +501,7 @@ class CDevNetParams : public CChainParams {
503501
fRequireStandard = false;
504502
fMineBlocksOnDemand = false;
505503
fAllowMultipleAddressesFromGroup = true;
506-
fAllowMultiplePorts = false;
504+
fAllowMultiplePorts = true;
507505

508506
nPoolMaxTransactions = 3;
509507
nFulfilledRequestExpireTime = 5*60; // fulfilled requests expire in 5 minutes

src/net.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,10 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce)
348348
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
349349
{
350350
if (pszDest == NULL) {
351-
if (IsLocal(addrConnect))
351+
bool fAllowLocal = Params().AllowMultiplePorts() && addrConnect.GetPort() != GetListenPort();
352+
if (!fAllowLocal && IsLocal(addrConnect)) {
352353
return NULL;
354+
}
353355

354356
// Look for an existing connection
355357
CNode* pnode = FindNode((CService)addrConnect);
@@ -1786,7 +1788,12 @@ void CConnman::ThreadOpenConnections()
17861788
CAddrInfo addr = addrman.Select(fFeeler);
17871789

17881790
// if we selected an invalid address, restart
1789-
if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
1791+
if (!addr.IsValid() || setConnected.count(addr.GetGroup()))
1792+
break;
1793+
1794+
// if we selected a local address, restart (local addresses are allowed in regtest and devnet)
1795+
bool fAllowLocal = Params().AllowMultiplePorts() && addrConnect.GetPort() != GetListenPort();
1796+
if (!fAllowLocal && IsLocal(addrConnect))
17901797
break;
17911798

17921799
// If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
@@ -1818,7 +1825,7 @@ void CConnman::ThreadOpenConnections()
18181825
}
18191826

18201827
// do not allow non-default ports, unless after 50 invalid addresses selected already
1821-
if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
1828+
if (addr.GetPort() != Params().GetDefaultPort() && addr.GetPort() != GetListenPort() && nTries < 50)
18221829
continue;
18231830

18241831
addrConnect = addr;
@@ -1990,9 +1997,16 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
19901997
return false;
19911998
}
19921999
if (!pszDest) {
1993-
if (IsLocal(addrConnect) ||
1994-
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
1995-
FindNode(addrConnect.ToStringIPPort()))
2000+
// banned or exact match?
2001+
if (IsBanned(addrConnect) || FindNode(addrConnect.ToStringIPPort()))
2002+
return false;
2003+
// local and not a connection to itself?
2004+
bool fAllowLocal = Params().AllowMultiplePorts() && addrConnect.GetPort() != GetListenPort();
2005+
if (!fAllowLocal && IsLocal(addrConnect))
2006+
return false;
2007+
// if multiple ports for same IP are allowed, search for IP:PORT match, otherwise search for IP-only match
2008+
if ((!Params().AllowMultiplePorts() && FindNode((CNetAddr)addrConnect)) ||
2009+
(Params().AllowMultiplePorts() && FindNode((CService)addrConnect)))
19962010
return false;
19972011
} else if (FindNode(std::string(pszDest)))
19982012
return false;
@@ -2238,7 +2252,9 @@ void CConnman::SetNetworkActive(bool active)
22382252
uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
22392253
}
22402254

2241-
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
2255+
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) :
2256+
nSeed0(nSeed0In), nSeed1(nSeed1In),
2257+
addrman(Params().AllowMultiplePorts())
22422258
{
22432259
fNetworkActive = true;
22442260
setBannedIsDirty = false;

src/test/addrman_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CAddrManTest : public CAddrMan
3333
return (unsigned int)(state % nMax);
3434
}
3535

36-
CAddrInfo* Find(const CNetAddr& addr, int* pnId = NULL)
36+
CAddrInfo* Find(const CService& addr, int* pnId = NULL)
3737
{
3838
return CAddrMan::Find(addr, pnId);
3939
}

0 commit comments

Comments
 (0)