Skip to content

Commit 2b08c55

Browse files
committed
[refactor] Add CChainParams member to CConnman
This is done in preparation to the next commit, but has the nice effect of removing one further data structure relying on the global `Params()`.
1 parent f0d1d8b commit 2b08c55

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12231223
assert(!node.connman);
12241224
node.connman = std::make_unique<CConnman>(GetRand<uint64_t>(),
12251225
GetRand<uint64_t>(),
1226-
*node.addrman, *node.netgroupman, args.GetBoolArg("-networkactive", true));
1226+
*node.addrman, *node.netgroupman, chainparams, args.GetBoolArg("-networkactive", true));
12271227

12281228
assert(!node.fee_estimator);
12291229
// Don't initialize fee estimation with old data if we don't relay transactions,

src/net.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -470,8 +470,8 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
470470
Ticks<HoursDouble>(pszDest ? 0h : Now<NodeSeconds>() - addrConnect.nTime));
471471

472472
// Resolve
473-
const uint16_t default_port{pszDest != nullptr ? Params().GetDefaultPort(pszDest) :
474-
Params().GetDefaultPort()};
473+
const uint16_t default_port{pszDest != nullptr ? m_params.GetDefaultPort(pszDest) :
474+
m_params.GetDefaultPort()};
475475
if (pszDest) {
476476
const std::vector<CService> resolved{Lookup(pszDest, default_port, fNameLookup && !HaveNameProxy(), 256)};
477477
if (!resolved.empty()) {
@@ -2184,7 +2184,7 @@ void CConnman::WakeMessageHandler()
21842184
void CConnman::ThreadDNSAddressSeed()
21852185
{
21862186
FastRandomContext rng;
2187-
std::vector<std::string> seeds = Params().DNSSeeds();
2187+
std::vector<std::string> seeds = m_params.DNSSeeds();
21882188
Shuffle(seeds.begin(), seeds.end(), rng);
21892189
int seeds_right_now = 0; // Number of seeds left before testing if we have enough connections
21902190
int found = 0;
@@ -2275,7 +2275,7 @@ void CConnman::ThreadDNSAddressSeed()
22752275
const auto addresses{LookupHost(host, nMaxIPs, true)};
22762276
if (!addresses.empty()) {
22772277
for (const CNetAddr& ip : addresses) {
2278-
CAddress addr = CAddress(CService(ip, Params().GetDefaultPort()), requiredServiceBits);
2278+
CAddress addr = CAddress(CService(ip, m_params.GetDefaultPort()), requiredServiceBits);
22792279
addr.nTime = rng.rand_uniform_delay(Now<NodeSeconds>() - 3 * 24h, -4 * 24h); // use a random age between 3 and 7 days old
22802280
vAdd.push_back(addr);
22812281
found++;
@@ -2480,7 +2480,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
24802480
}
24812481

24822482
if (add_fixed_seeds_now) {
2483-
std::vector<CAddress> seed_addrs{ConvertSeeds(Params().FixedSeeds())};
2483+
std::vector<CAddress> seed_addrs{ConvertSeeds(m_params.FixedSeeds())};
24842484
// We will not make outgoing connections to peers that are unreachable
24852485
// (e.g. because of -onlynet configuration).
24862486
// Therefore, we do not add them to addrman in the first place.
@@ -2769,7 +2769,7 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo() const
27692769
}
27702770

27712771
for (const std::string& strAddNode : lAddresses) {
2772-
CService service(LookupNumeric(strAddNode, Params().GetDefaultPort(strAddNode)));
2772+
CService service(LookupNumeric(strAddNode, m_params.GetDefaultPort(strAddNode)));
27732773
AddedNodeInfo addedNode{strAddNode, CService(), false, false};
27742774
if (service.IsValid()) {
27752775
// strAddNode is an IP:port
@@ -3075,11 +3075,12 @@ void CConnman::SetNetworkActive(bool active)
30753075
}
30763076

30773077
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, AddrMan& addrman_in,
3078-
const NetGroupManager& netgroupman, bool network_active)
3078+
const NetGroupManager& netgroupman, const CChainParams& params, bool network_active)
30793079
: addrman(addrman_in)
30803080
, m_netgroupman{netgroupman}
30813081
, nSeed0(nSeed0In)
30823082
, nSeed1(nSeed1In)
3083+
, m_params(params)
30833084
{
30843085
SetTryNewOutboundPeer(false);
30853086

src/net.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
class AddrMan;
4949
class BanMan;
50+
class CChainParams;
5051
class CNode;
5152
class CScheduler;
5253
struct bilingual_str;
@@ -1081,7 +1082,7 @@ class CConnman
10811082
}
10821083

10831084
CConnman(uint64_t seed0, uint64_t seed1, AddrMan& addrman, const NetGroupManager& netgroupman,
1084-
bool network_active = true);
1085+
const CChainParams& params, bool network_active = true);
10851086

10861087
~CConnman();
10871088

@@ -1566,6 +1567,8 @@ class CConnman
15661567
std::vector<CNode*> m_nodes_copy;
15671568
};
15681569

1570+
const CChainParams& m_params;
1571+
15691572
friend struct ConnmanTestMsg;
15701573
};
15711574

src/test/denialofservice_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static void AddRandomOutboundPeer(NodeId& id, std::vector<CNode*>& vNodes, PeerM
142142
BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
143143
{
144144
NodeId id{0};
145-
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
145+
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params());
146146
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr, *m_node.chainman, *m_node.mempool, {});
147147

148148
constexpr int max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS;
@@ -242,7 +242,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
242242
BOOST_AUTO_TEST_CASE(block_relay_only_eviction)
243243
{
244244
NodeId id{0};
245-
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
245+
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params());
246246
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, nullptr, *m_node.chainman, *m_node.mempool, {});
247247

248248
constexpr int max_outbound_block_relay{MAX_BLOCK_RELAY_ONLY_CONNECTIONS};
@@ -305,7 +305,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
305305
LOCK(NetEventsInterface::g_msgproc_mutex);
306306

307307
auto banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME);
308-
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
308+
auto connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params());
309309
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, banman.get(), *m_node.chainman, *m_node.mempool, {});
310310

311311
CNetAddr tor_netaddr;
@@ -407,7 +407,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
407407
LOCK(NetEventsInterface::g_msgproc_mutex);
408408

409409
auto banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME);
410-
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman);
410+
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params());
411411
auto peerLogic = PeerManager::make(*connman, *m_node.addrman, banman.get(), *m_node.chainman, *m_node.mempool, {});
412412

413413
banman->ClearBanned();

src/test/fuzz/connman.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ FUZZ_TARGET(connman, .init = initialize_connman)
3636
fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
3737
*g_setup->m_node.addrman,
3838
*g_setup->m_node.netgroupman,
39+
Params(),
3940
fuzzed_data_provider.ConsumeBool()};
4041
CNetAddr random_netaddr;
4142
CNode random_node = ConsumeNode(fuzzed_data_provider);

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ TestingSetup::TestingSetup(
253253
/*deterministic=*/false,
254254
m_node.args->GetIntArg("-checkaddrman", 0));
255255
m_node.banman = std::make_unique<BanMan>(m_args.GetDataDirBase() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME);
256-
m_node.connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman); // Deterministic randomness for tests.
256+
m_node.connman = std::make_unique<ConnmanTestMsg>(0x1337, 0x1337, *m_node.addrman, *m_node.netgroupman, Params()); // Deterministic randomness for tests.
257257
PeerManager::Options peerman_opts;
258258
ApplyArgsManOptions(*m_node.args, peerman_opts);
259259
m_node.peerman = PeerManager::make(*m_node.connman, *m_node.addrman,

0 commit comments

Comments
 (0)