Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/addrman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ double CAddrInfo::GetChance(int64_t nNow) const
return fChance;
}

CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int* pnId)
CAddrInfo* CAddrMan::Find(const CService& addr, int* pnId)
{
std::map<CNetAddr, int>::iterator it = mapAddr.find(addr);
CService addr2 = addr;
if (!discriminatePorts) {
addr2.SetPort(0);
}

std::map<CService, int>::iterator it = mapAddr.find(addr2);
if (it == mapAddr.end())
return NULL;
if (pnId)
Expand All @@ -80,9 +85,14 @@ CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int* pnId)

CAddrInfo* CAddrMan::Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId)
{
CService addr2 = addr;
if (!discriminatePorts) {
addr2.SetPort(0);
}

int nId = nIdCount++;
mapInfo[nId] = CAddrInfo(addr, addrSource);
mapAddr[addr] = nId;
mapAddr[addr2] = nId;
mapInfo[nId].nRandomPos = vRandom.size();
vRandom.push_back(nId);
if (pnId)
Expand Down Expand Up @@ -117,9 +127,14 @@ void CAddrMan::Delete(int nId)
assert(!info.fInTried);
assert(info.nRefCount == 0);

CService addr = info;
if (!discriminatePorts) {
addr.SetPort(0);
}

SwapRandom(info.nRandomPos, vRandom.size() - 1);
vRandom.pop_back();
mapAddr.erase(info);
mapAddr.erase(addr);
mapInfo.erase(nId);
nNew--;
}
Expand Down
10 changes: 7 additions & 3 deletions src/addrman.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class CAddrMan
std::map<int, CAddrInfo> mapInfo;

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

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

// discriminate entries based on port. Should be false on mainnet/testnet and can be true on devnet/regtest
bool discriminatePorts;

protected:
//! secret key to randomize bucket select with
uint256 nKey;
Expand All @@ -215,7 +218,7 @@ class CAddrMan
FastRandomContext insecure_rand;

//! Find an entry.
CAddrInfo* Find(const CNetAddr& addr, int *pnId = NULL);
CAddrInfo* Find(const CService& addr, int *pnId = NULL);

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

CAddrMan()
CAddrMan(bool _discriminatePorts = false) :
discriminatePorts(_discriminatePorts)
{
Clear();
}
Expand Down
4 changes: 1 addition & 3 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,6 @@ class CTestNetParams : public CChainParams {
// Testnet Dash BIP44 coin type is '1' (All coin's testnet default)
nExtCoinType = 1;

vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test));

fMiningRequiresPeers = true;
fDefaultConsistencyChecks = false;
fRequireStandard = false;
Expand Down Expand Up @@ -503,7 +501,7 @@ class CDevNetParams : public CChainParams {
fRequireStandard = false;
fMineBlocksOnDemand = false;
fAllowMultipleAddressesFromGroup = true;
fAllowMultiplePorts = false;
fAllowMultiplePorts = true;

nPoolMaxTransactions = 3;
nFulfilledRequestExpireTime = 5*60; // fulfilled requests expire in 5 minutes
Expand Down
21 changes: 16 additions & 5 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce)
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
{
if (pszDest == NULL) {
if (IsLocal(addrConnect))
if (IsLocal(addrConnect) && (!Params().AllowMultiplePorts() || addrConnect.GetPort() == GetListenPort())) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's too hard to read, maybe

bool fAllowLocal = Params().AllowMultiplePorts() && addrConnect.GetPort() != GetListenPort();
if (!fAllowLocal && IsLocal(addrConnect)) {

?

return NULL;
}

// Look for an existing connection
CNode* pnode = FindNode((CService)addrConnect);
Expand Down Expand Up @@ -1786,7 +1787,11 @@ void CConnman::ThreadOpenConnections()
CAddrInfo addr = addrman.Select(fFeeler);

// if we selected an invalid address, restart
if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
if (!addr.IsValid() || setConnected.count(addr.GetGroup()))
break;

// if we selected a local address, restart (local addresses are allowed in regtest and devnet)
if (IsLocal(addr) && (!Params().AllowMultiplePorts() || addr.GetPort() == GetListenPort()))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

break;

// If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
Expand Down Expand Up @@ -1990,10 +1995,14 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
return false;
}
if (!pszDest) {
if (IsLocal(addrConnect) ||
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
if (IsBanned(addrConnect) ||
FindNode(addrConnect.ToStringIPPort()))
return false;
if (IsLocal(addrConnect) && (!Params().AllowMultiplePorts() || addrConnect.GetPort() == GetListenPort()))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

bool fAllowLocal = Params().AllowMultiplePorts() && addrConnect.GetPort() != GetListenPort();
if ((!fAllowLocal && IsLocal(addrConnect)) ||
    (!Params().AllowMultiplePorts() && FindNode((CNetAddr)addrConnect)) ||
    (Params().AllowMultiplePorts() && FindNode((CService)addrConnect)) ||
    IsBanned(addrConnect) ||
    FindNode(addrConnect.ToStringIPPort()))
    return false;

return false;
if ((!Params().AllowMultiplePorts() && FindNode((CNetAddr)addrConnect)) ||
(Params().AllowMultiplePorts() && FindNode((CService)addrConnect)))
return false;
} else if (FindNode(std::string(pszDest)))
return false;

Expand Down Expand Up @@ -2238,7 +2247,9 @@ void CConnman::SetNetworkActive(bool active)
uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
}

CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) :
nSeed0(nSeed0In), nSeed1(nSeed1In),
addrman(Params().AllowMultiplePorts())
{
fNetworkActive = true;
setBannedIsDirty = false;
Expand Down
2 changes: 1 addition & 1 deletion src/test/addrman_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CAddrManTest : public CAddrMan
return (unsigned int)(state % nMax);
}

CAddrInfo* Find(const CNetAddr& addr, int* pnId = NULL)
CAddrInfo* Find(const CService& addr, int* pnId = NULL)
{
return CAddrMan::Find(addr, pnId);
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/net_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(caddrdb_read)
// Test that the de-serialization does not throw an exception.
CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted);
bool exceptionThrown = false;
CAddrMan addrman1;
CAddrMan addrman1(false);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed, false is the default one (same for all changes below in this file).


BOOST_CHECK(addrman1.size() == 0);
try {
Expand All @@ -109,7 +109,7 @@ BOOST_AUTO_TEST_CASE(caddrdb_read)
// Test that CAddrDB::Read creates an addrman with the correct number of addrs.
CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted);

CAddrMan addrman2;
CAddrMan addrman2(false);
CAddrDB adb;
BOOST_CHECK(addrman2.size() == 0);
adb.Read(addrman2, ssPeers2);
Expand All @@ -125,7 +125,7 @@ BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
// Test that the de-serialization of corrupted addrman throws an exception.
CDataStream ssPeers1 = AddrmanToStream(addrmanCorrupted);
bool exceptionThrown = false;
CAddrMan addrman1;
CAddrMan addrman1(false);
BOOST_CHECK(addrman1.size() == 0);
try {
unsigned char pchMsgTmp[4];
Expand All @@ -141,7 +141,7 @@ BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
// Test that CAddrDB::Read leaves addrman in a clean state if de-serialization fails.
CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted);

CAddrMan addrman2;
CAddrMan addrman2(false);
CAddrDB adb;
BOOST_CHECK(addrman2.size() == 0);
adb.Read(addrman2, ssPeers2);
Expand Down