Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 23 additions & 7 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,10 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce)
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
{
if (pszDest == NULL) {
if (IsLocal(addrConnect))
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 +1788,12 @@ 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)
bool fAllowLocal = Params().AllowMultiplePorts() && addrConnect.GetPort() != GetListenPort();
if (!fAllowLocal && IsLocal(addrConnect))
break;

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

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

addrConnect = addr;
Expand Down Expand Up @@ -1990,9 +1997,16 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
return false;
}
if (!pszDest) {
if (IsLocal(addrConnect) ||
FindNode((CNetAddr)addrConnect) || IsBanned(addrConnect) ||
FindNode(addrConnect.ToStringIPPort()))
// banned or exact match?
if (IsBanned(addrConnect) || FindNode(addrConnect.ToStringIPPort()))
return false;
// local and not a connection to itself?
bool fAllowLocal = Params().AllowMultiplePorts() && addrConnect.GetPort() != GetListenPort();
if (!fAllowLocal && IsLocal(addrConnect))
return false;
// if multiple ports for same IP are allowed, search for IP:PORT match, otherwise search for IP-only match
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 +2252,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