Skip to content

Commit 047ded0

Browse files
committed
Merge #8688: Move static global randomizer seeds into CConnman
d9ff591 Move static global randomizer seeds into CConnman (Pieter Wuille)
2 parents 894c0ad + d9ff591 commit 047ded0

File tree

7 files changed

+34
-24
lines changed

7 files changed

+34
-24
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
11161116
// ********************************************************* Step 6: network initialization
11171117

11181118
assert(!g_connman);
1119-
g_connman = std::unique_ptr<CConnman>(new CConnman());
1119+
g_connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
11201120
CConnman& connman = *g_connman;
11211121

11221122
RegisterNodeSignals(GetNodeSignals());

src/main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ CScript COINBASE_FLAGS;
113113

114114
const string strMessageMagic = "Bitcoin Signed Message:\n";
115115

116+
static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL; // SHA256("main address relay")[0:8]
117+
116118
// Internal stuff
117119
namespace {
118120

@@ -4739,11 +4741,9 @@ static void RelayAddress(const CAddress& addr, bool fReachable, CConnman& connma
47394741
// Relay to a limited number of other nodes
47404742
// Use deterministic randomness to send to the same nodes for 24 hours
47414743
// at a time so the addrKnowns of the chosen nodes prevent repeats
4742-
static const uint64_t salt0 = GetRand(std::numeric_limits<uint64_t>::max());
4743-
static const uint64_t salt1 = GetRand(std::numeric_limits<uint64_t>::max());
47444744
uint64_t hashAddr = addr.GetHash();
47454745
std::multimap<uint64_t, CNode*> mapMix;
4746-
const CSipHasher hasher = CSipHasher(salt0, salt1).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
4746+
const CSipHasher hasher = connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
47474747

47484748
auto sortfunc = [&mapMix, &hasher](CNode* pnode) {
47494749
if (pnode->nVersion >= CADDR_TIME_VERSION) {

src/net.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363

6464
const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
6565

66+
static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL; // SHA256("netgroup")[0:8]
6667
//
6768
// Global state variables
6869
//
@@ -387,7 +388,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
387388
addrman.Attempt(addrConnect, fCountFailure);
388389

389390
// Add node
390-
CNode* pnode = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), hSocket, addrConnect, pszDest ? pszDest : "", false);
391+
CNode* pnode = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), pszDest ? pszDest : "", false);
391392
GetNodeSignals().InitializeNode(pnode->GetId(), pnode);
392393
pnode->AddRef();
393394

@@ -1022,7 +1023,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10221023
}
10231024
}
10241025

1025-
CNode* pnode = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), hSocket, addr, "", true);
1026+
CNode* pnode = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), "", true);
10261027
GetNodeSignals().InitializeNode(pnode->GetId(), pnode);
10271028
pnode->AddRef();
10281029
pnode->fWhitelisted = whitelisted;
@@ -2023,7 +2024,7 @@ void Discover(boost::thread_group& threadGroup)
20232024
#endif
20242025
}
20252026

2026-
CConnman::CConnman()
2027+
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSeed1(nSeed1In)
20272028
{
20282029
setBannedIsDirty = false;
20292030
fAddressesInitialized = false;
@@ -2109,7 +2110,7 @@ bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, st
21092110
if (pnodeLocalHost == NULL) {
21102111
CNetAddr local;
21112112
LookupHost("127.0.0.1", local, false);
2112-
pnodeLocalHost = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices));
2113+
pnodeLocalHost = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices), 0);
21132114
GetNodeSignals().InitializeNode(pnodeLocalHost->GetId(), pnodeLocalHost);
21142115
}
21152116

@@ -2499,10 +2500,10 @@ void CNode::Fuzz(int nChance)
24992500
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
25002501
unsigned int CConnman::GetSendBufferSize() const{ return nSendBufferMaxSize; }
25012502

2502-
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, const std::string& addrNameIn, bool fInboundIn) :
2503+
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, const std::string& addrNameIn, bool fInboundIn) :
25032504
ssSend(SER_NETWORK, INIT_PROTO_VERSION),
25042505
addr(addrIn),
2505-
nKeyedNetGroup(CalculateKeyedNetGroup(addrIn)),
2506+
nKeyedNetGroup(nKeyedNetGroupIn),
25062507
addrKnown(5000, 0.001),
25072508
filterInventoryKnown(50000, 0.000001)
25082509
{
@@ -2695,12 +2696,14 @@ int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
26952696
return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
26962697
}
26972698

2698-
/* static */ uint64_t CNode::CalculateKeyedNetGroup(const CAddress& ad)
2699+
CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id)
26992700
{
2700-
static const uint64_t k0 = GetRand(std::numeric_limits<uint64_t>::max());
2701-
static const uint64_t k1 = GetRand(std::numeric_limits<uint64_t>::max());
2701+
return CSipHasher(nSeed0, nSeed1).Write(id);
2702+
}
27022703

2704+
uint64_t CConnman::CalculateKeyedNetGroup(const CAddress& ad)
2705+
{
27032706
std::vector<unsigned char> vchNetGroup(ad.GetGroup());
27042707

2705-
return CSipHasher(k0, k1).Write(&vchNetGroup[0], vchNetGroup.size()).Finalize();
2708+
return GetDeterministicRandomizer(RANDOMIZER_ID_NETGROUP).Write(&vchNetGroup[0], vchNetGroup.size()).Finalize();
27062709
}

src/net.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "amount.h"
1212
#include "bloom.h"
1313
#include "compat.h"
14+
#include "hash.h"
1415
#include "limitedmap.h"
1516
#include "netaddress.h"
1617
#include "protocol.h"
@@ -125,7 +126,7 @@ class CConnman
125126
uint64_t nMaxOutboundTimeframe = 0;
126127
uint64_t nMaxOutboundLimit = 0;
127128
};
128-
CConnman();
129+
CConnman(uint64_t seed0, uint64_t seed1);
129130
~CConnman();
130131
bool Start(boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options options);
131132
void Stop();
@@ -298,6 +299,8 @@ class CConnman
298299
void SetBestHeight(int height);
299300
int GetBestHeight() const;
300301

302+
/** Get a unique deterministic randomizer. */
303+
CSipHasher GetDeterministicRandomizer(uint64_t id);
301304

302305
private:
303306
struct ListenSocket {
@@ -315,6 +318,8 @@ class CConnman
315318
void ThreadSocketHandler();
316319
void ThreadDNSAddressSeed();
317320

321+
uint64_t CalculateKeyedNetGroup(const CAddress& ad);
322+
318323
CNode* FindNode(const CNetAddr& ip);
319324
CNode* FindNode(const CSubNet& subNet);
320325
CNode* FindNode(const std::string& addrName);
@@ -392,6 +397,9 @@ class CConnman
392397
int nMaxFeeler;
393398
std::atomic<int> nBestHeight;
394399
CClientUIInterface* clientInterface;
400+
401+
/** SipHasher seeds for deterministic randomness */
402+
const uint64_t nSeed0, nSeed1;
395403
};
396404
extern std::unique_ptr<CConnman> g_connman;
397405
void Discover(boost::thread_group& threadGroup);
@@ -660,14 +668,13 @@ class CNode
660668
CAmount lastSentFeeFilter;
661669
int64_t nextSendTimeFeeFilter;
662670

663-
CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, const std::string &addrNameIn = "", bool fInboundIn = false);
671+
CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, const std::string &addrNameIn = "", bool fInboundIn = false);
664672
~CNode();
665673

666674
private:
667675
CNode(const CNode&);
668676
void operator=(const CNode&);
669677

670-
static uint64_t CalculateKeyedNetGroup(const CAddress& ad);
671678

672679
uint64_t nLocalHostNonce;
673680
ServiceFlags nLocalServices;

src/test/DoS_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
4848
{
4949
connman->ClearBanned();
5050
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
51-
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, "", true);
51+
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, 0, "", true);
5252
GetNodeSignals().InitializeNode(dummyNode1.GetId(), &dummyNode1);
5353
dummyNode1.nVersion = 1;
5454
Misbehaving(dummyNode1.GetId(), 100); // Should get banned
@@ -57,7 +57,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
5757
BOOST_CHECK(!connman->IsBanned(ip(0xa0b0c001|0x0000ff00))); // Different IP, not banned
5858

5959
CAddress addr2(ip(0xa0b0c002), NODE_NONE);
60-
CNode dummyNode2(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr2, "", true);
60+
CNode dummyNode2(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr2, 1, "", true);
6161
GetNodeSignals().InitializeNode(dummyNode2.GetId(), &dummyNode2);
6262
dummyNode2.nVersion = 1;
6363
Misbehaving(dummyNode2.GetId(), 50);
@@ -74,7 +74,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
7474
connman->ClearBanned();
7575
mapArgs["-banscore"] = "111"; // because 11 is my favorite number
7676
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
77-
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, "", true);
77+
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, 3, "", true);
7878
GetNodeSignals().InitializeNode(dummyNode1.GetId(), &dummyNode1);
7979
dummyNode1.nVersion = 1;
8080
Misbehaving(dummyNode1.GetId(), 100);
@@ -96,7 +96,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
9696
SetMockTime(nStartTime); // Overrides future calls to GetTime()
9797

9898
CAddress addr(ip(0xa0b0c001), NODE_NONE);
99-
CNode dummyNode(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr, "", true);
99+
CNode dummyNode(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr, 4, "", true);
100100
GetNodeSignals().InitializeNode(dummyNode.GetId(), &dummyNode);
101101
dummyNode.nVersion = 1;
102102

src/test/net_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ BOOST_AUTO_TEST_CASE(cnode_simple_test)
164164
bool fInboundIn = false;
165165

166166
// Test that fFeeler is false by default.
167-
CNode* pnode1 = new CNode(id++, NODE_NETWORK, height, hSocket, addr, pszDest, fInboundIn);
167+
CNode* pnode1 = new CNode(id++, NODE_NETWORK, height, hSocket, addr, 0, pszDest, fInboundIn);
168168
BOOST_CHECK(pnode1->fInbound == false);
169169
BOOST_CHECK(pnode1->fFeeler == false);
170170

171171
fInboundIn = true;
172-
CNode* pnode2 = new CNode(id++, NODE_NETWORK, height, hSocket, addr, pszDest, fInboundIn);
172+
CNode* pnode2 = new CNode(id++, NODE_NETWORK, height, hSocket, addr, 1, pszDest, fInboundIn);
173173
BOOST_CHECK(pnode2->fInbound == true);
174174
BOOST_CHECK(pnode2->fFeeler == false);
175175
}

src/test/test_bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
7272
nScriptCheckThreads = 3;
7373
for (int i=0; i < nScriptCheckThreads-1; i++)
7474
threadGroup.create_thread(&ThreadScriptCheck);
75-
g_connman = std::unique_ptr<CConnman>(new CConnman());
75+
g_connman = std::unique_ptr<CConnman>(new CConnman(0x1337, 0x1337)); // Deterministic randomness for tests.
7676
connman = g_connman.get();
7777
RegisterNodeSignals(GetNodeSignals());
7878
}

0 commit comments

Comments
 (0)