Skip to content

Commit 44dbf91

Browse files
committed
Merge bitcoin/bitcoin#24627: test: Limit scope of id global which is shared between subtests
fa9086d test: Limit scope of id global which is shared between subtests (MarcoFalke) Pull request description: Globals aren't too nice when testing, as leak state between subtests run in the same process. For example, when checking peer ids in the tests, they might pass/fail depending on other tests run in the same process. Fix this by making `id` not a global. ACKs for top commit: promag: Code review ACK fa9086d. Tree-SHA512: 0a53dde428570086f4557b23112e6460d6413bedf6ef487bd56e88f83cd5f4526f44effa8076cdeaf4761ecc062c346948e0bff434808bbf9b558eabd81328e3
2 parents 138d55e + fa9086d commit 44dbf91

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/test/denialofservice_tests.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ static CService ip(uint32_t i)
3434
return CService(CNetAddr(s), Params().GetDefaultPort());
3535
}
3636

37-
static NodeId id = 0;
38-
3937
void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds);
4038

4139
BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
@@ -59,6 +57,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
5957

6058
// Mock an outbound peer
6159
CAddress addr1(ip(0xa0b0c001), NODE_NONE);
60+
NodeId id{0};
6261
CNode dummyNode1{id++,
6362
ServiceFlags(NODE_NETWORK | NODE_WITNESS),
6463
/*sock=*/nullptr,
@@ -114,7 +113,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
114113
peerLogic->FinalizeNode(dummyNode1);
115114
}
116115

117-
static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connType)
116+
static void AddRandomOutboundPeer(NodeId& id, std::vector<CNode*>& vNodes, PeerManager& peerLogic, ConnmanTestMsg& connman, ConnectionType connType)
118117
{
119118
CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE);
120119
vNodes.emplace_back(new CNode{id++,
@@ -138,6 +137,7 @@ static void AddRandomOutboundPeer(std::vector<CNode*>& vNodes, PeerManager& peer
138137

139138
BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
140139
{
140+
NodeId id{0};
141141
const CChainParams& chainparams = Params();
142142
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman);
143143
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr,
@@ -157,7 +157,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
157157

158158
// Mock some outbound peers
159159
for (int i = 0; i < max_outbound_full_relay; ++i) {
160-
AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY);
160+
AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY);
161161
}
162162

163163
peerLogic->CheckForStaleTipAndEvictPeers();
@@ -183,7 +183,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
183183
// on the next check (since we're mocking the time to be in the future, the
184184
// required time connected check should be satisfied).
185185
SetMockTime(time_init);
186-
AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY);
186+
AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::OUTBOUND_FULL_RELAY);
187187
SetMockTime(time_later);
188188

189189
peerLogic->CheckForStaleTipAndEvictPeers();
@@ -215,6 +215,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
215215

216216
BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
217217
{
218+
NodeId id{0};
218219
const CChainParams& chainparams = Params();
219220
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman);
220221
auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr,
@@ -232,7 +233,7 @@ BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
232233

233234
// Add block-relay-only peers up to the limit
234235
for (int i = 0; i < max_outbound_block_relay; ++i) {
235-
AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY);
236+
AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY);
236237
}
237238
peerLogic->CheckForStaleTipAndEvictPeers();
238239

@@ -241,7 +242,7 @@ BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
241242
}
242243

243244
// Add an extra block-relay-only peer breaking the limit (mocks logic in ThreadOpenConnections)
244-
AddRandomOutboundPeer(vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY);
245+
AddRandomOutboundPeer(id, vNodes, *peerLogic, *connman, ConnectionType::BLOCK_RELAY);
245246
peerLogic->CheckForStaleTipAndEvictPeers();
246247

247248
// The extra peer should only get marked for eviction after MINIMUM_CONNECT_TIME
@@ -297,6 +298,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
297298
std::array<CNode*, 3> nodes;
298299

299300
banman->ClearBanned();
301+
NodeId id{0};
300302
nodes[0] = new CNode{id++,
301303
NODE_NETWORK,
302304
/*sock=*/nullptr,
@@ -403,6 +405,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
403405
SetMockTime(nStartTime); // Overrides future calls to GetTime()
404406

405407
CAddress addr(ip(0xa0b0c001), NODE_NONE);
408+
NodeId id{0};
406409
CNode dummyNode{id++,
407410
NODE_NETWORK,
408411
/*sock=*/nullptr,

0 commit comments

Comments
 (0)