Skip to content

Commit fcf61b8

Browse files
committed
Merge #9050: net: make a few values immutable, and use deterministic randomness for the localnonce
59ac5c5 net: Use deterministic randomness for CNode's nonce, and make it const (Cory Fields) aff6584 net: constify a few CNode vars to indicate that they're threadsafe (Cory Fields)
2 parents d1871da + 59ac5c5 commit fcf61b8

File tree

4 files changed

+33
-23
lines changed

4 files changed

+33
-23
lines changed

src/net.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
const static std::string NET_MESSAGE_COMMAND_OTHER = "*other*";
6565

6666
static const uint64_t RANDOMIZER_ID_NETGROUP = 0x6c0edd8036ef4036ULL; // SHA256("netgroup")[0:8]
67+
static const uint64_t RANDOMIZER_ID_LOCALHOSTNONCE = 0xd93e69e2bbfa5735ULL; // SHA256("localhostnonce")[0:8]
6768
//
6869
// Global state variables
6970
//
@@ -389,7 +390,10 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
389390
addrman.Attempt(addrConnect, fCountFailure);
390391

391392
// Add node
392-
CNode* pnode = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), pszDest ? pszDest : "", false);
393+
NodeId id = GetNewNodeId();
394+
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
395+
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, pszDest ? pszDest : "", false);
396+
393397
GetNodeSignals().InitializeNode(pnode->GetId(), pnode);
394398
pnode->AddRef();
395399

@@ -1024,7 +1028,10 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10241028
}
10251029
}
10261030

1027-
CNode* pnode = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), "", true);
1031+
NodeId id = GetNewNodeId();
1032+
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
1033+
1034+
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, "", true);
10281035
GetNodeSignals().InitializeNode(pnode->GetId(), pnode);
10291036
pnode->AddRef();
10301037
pnode->fWhitelisted = whitelisted;
@@ -2118,7 +2125,11 @@ bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, st
21182125
if (pnodeLocalHost == NULL) {
21192126
CNetAddr local;
21202127
LookupHost("127.0.0.1", local, false);
2121-
pnodeLocalHost = new CNode(GetNewNodeId(), nLocalServices, GetBestHeight(), INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices), 0);
2128+
2129+
NodeId id = GetNewNodeId();
2130+
uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize();
2131+
2132+
pnodeLocalHost = new CNode(id, nLocalServices, GetBestHeight(), INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices), 0, nonce);
21222133
GetNodeSignals().InitializeNode(pnodeLocalHost->GetId(), pnodeLocalHost);
21232134
}
21242135

@@ -2509,12 +2520,17 @@ void CNode::Fuzz(int nChance)
25092520
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
25102521
unsigned int CConnman::GetSendBufferSize() const{ return nSendBufferMaxSize; }
25112522

2512-
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, const std::string& addrNameIn, bool fInboundIn) :
2523+
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const std::string& addrNameIn, bool fInboundIn) :
25132524
ssSend(SER_NETWORK, INIT_PROTO_VERSION),
25142525
addr(addrIn),
2526+
fInbound(fInboundIn),
2527+
id(idIn),
25152528
nKeyedNetGroup(nKeyedNetGroupIn),
25162529
addrKnown(5000, 0.001),
2517-
filterInventoryKnown(50000, 0.000001)
2530+
filterInventoryKnown(50000, 0.000001),
2531+
nLocalHostNonce(nLocalHostNonceIn),
2532+
nLocalServices(nLocalServicesIn),
2533+
nMyStartingHeight(nMyStartingHeightIn)
25182534
{
25192535
nServices = NODE_NONE;
25202536
nServicesExpected = NODE_NONE;
@@ -2533,7 +2549,6 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
25332549
fOneShot = false;
25342550
fClient = false; // set by version message
25352551
fFeeler = false;
2536-
fInbound = fInboundIn;
25372552
fNetworkNode = false;
25382553
fSuccessfullyConnected = false;
25392554
fDisconnect = false;
@@ -2562,12 +2577,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
25622577
minFeeFilter = 0;
25632578
lastSentFeeFilter = 0;
25642579
nextSendTimeFeeFilter = 0;
2565-
id = idIn;
25662580
nOptimisticBytesWritten = 0;
2567-
nLocalServices = nLocalServicesIn;
2568-
2569-
GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
2570-
nMyStartingHeight = nMyStartingHeightIn;
25712581

25722582
BOOST_FOREACH(const std::string &msg, getAllNetMessageTypes())
25732583
mapRecvBytesPerMsgCmd[msg] = 0;

src/net.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ class CNode
589589
bool fFeeler; // If true this node is being used as a short lived feeler.
590590
bool fOneShot;
591591
bool fClient;
592-
bool fInbound;
592+
const bool fInbound;
593593
bool fNetworkNode;
594594
bool fSuccessfullyConnected;
595595
bool fDisconnect;
@@ -603,7 +603,7 @@ class CNode
603603
CCriticalSection cs_filter;
604604
CBloomFilter* pfilter;
605605
int nRefCount;
606-
NodeId id;
606+
const NodeId id;
607607

608608
const uint64_t nKeyedNetGroup;
609609
protected:
@@ -669,18 +669,18 @@ class CNode
669669
CAmount lastSentFeeFilter;
670670
int64_t nextSendTimeFeeFilter;
671671

672-
CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, const std::string &addrNameIn = "", bool fInboundIn = false);
672+
CNode(NodeId id, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn, SOCKET hSocketIn, const CAddress &addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const std::string &addrNameIn = "", bool fInboundIn = false);
673673
~CNode();
674674

675675
private:
676676
CNode(const CNode&);
677677
void operator=(const CNode&);
678678

679679

680-
uint64_t nLocalHostNonce;
680+
const uint64_t nLocalHostNonce;
681681
// Services offered to this peer
682-
ServiceFlags nLocalServices;
683-
int nMyStartingHeight;
682+
const ServiceFlags nLocalServices;
683+
const int nMyStartingHeight;
684684
public:
685685

686686
NodeId GetId() const {

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, 0, "", true);
51+
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, 0, 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, 1, "", true);
60+
CNode dummyNode2(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr2, 1, 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, 3, "", true);
77+
CNode dummyNode1(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr1, 3, 1, "", 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, 4, "", true);
99+
CNode dummyNode(id++, NODE_NETWORK, 0, INVALID_SOCKET, addr, 4, 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, 0, pszDest, fInboundIn);
167+
CNode* pnode1 = new CNode(id++, NODE_NETWORK, height, hSocket, addr, 0, 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, 1, pszDest, fInboundIn);
172+
CNode* pnode2 = new CNode(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, pszDest, fInboundIn);
173173
BOOST_CHECK(pnode2->fInbound == true);
174174
BOOST_CHECK(pnode2->fFeeler == false);
175175
}

0 commit comments

Comments
 (0)