Skip to content

Commit 1c25adf

Browse files
committed
[net] Construct addrman outside connman
node.context owns the CAddrMan. CConnman holds a reference to the CAddrMan.
1 parent 3530d5d commit 1c25adf

File tree

8 files changed

+21
-11
lines changed

8 files changed

+21
-11
lines changed

src/init.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ void Shutdown(NodeContext& node)
228228
node.peerman.reset();
229229
node.connman.reset();
230230
node.banman.reset();
231+
node.addrman.reset();
231232

232233
if (node.mempool && node.mempool->IsLoaded() && node.args->GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
233234
DumpMempool(*node.mempool);
@@ -1402,10 +1403,12 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
14021403
fDiscover = args.GetBoolArg("-discover", true);
14031404
const bool ignores_incoming_txs{args.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY)};
14041405

1406+
assert(!node.addrman);
1407+
node.addrman = std::make_unique<CAddrMan>();
14051408
assert(!node.banman);
14061409
node.banman = std::make_unique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, args.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
14071410
assert(!node.connman);
1408-
node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), args.GetBoolArg("-networkactive", true));
1411+
node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), *node.addrman, args.GetBoolArg("-networkactive", true));
14091412

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

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,8 +2351,8 @@ void CConnman::SetNetworkActive(bool active)
23512351
uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
23522352
}
23532353

2354-
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, bool network_active)
2355-
: nSeed0(nSeed0In), nSeed1(nSeed1In)
2354+
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, CAddrMan& addrman_in, bool network_active)
2355+
: addrman(addrman_in), nSeed0(nSeed0In), nSeed1(nSeed1In)
23562356
{
23572357
SetTryNewOutboundPeer(false);
23582358

src/net.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ class CConnman
856856
m_onion_binds = connOptions.onion_binds;
857857
}
858858

859-
CConnman(uint64_t seed0, uint64_t seed1, bool network_active = true);
859+
CConnman(uint64_t seed0, uint64_t seed1, CAddrMan& addrman, bool network_active = true);
860860
~CConnman();
861861
bool Start(CScheduler& scheduler, const Options& options);
862862

@@ -1130,7 +1130,7 @@ class CConnman
11301130
std::vector<ListenSocket> vhListenSocket;
11311131
std::atomic<bool> fNetworkActive{true};
11321132
bool fAddressesInitialized{false};
1133-
CAddrMan addrman;
1133+
CAddrMan& addrman;
11341134
std::deque<std::string> m_addr_fetches GUARDED_BY(m_addr_fetches_mutex);
11351135
RecursiveMutex m_addr_fetches_mutex;
11361136
std::vector<std::string> vAddedNodes GUARDED_BY(cs_vAddedNodes);

src/node/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <node/context.h>
66

7+
#include <addrman.h>
78
#include <banman.h>
89
#include <interfaces/chain.h>
910
#include <net.h>

src/node/context.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
class ArgsManager;
1414
class BanMan;
15+
class CAddrMan;
1516
class CBlockPolicyEstimator;
1617
class CConnman;
1718
class CScheduler;
@@ -35,6 +36,7 @@ class WalletClient;
3536
//! any member functions. It should just be a collection of references that can
3637
//! be used without pulling in unwanted dependencies or functionality.
3738
struct NodeContext {
39+
std::unique_ptr<CAddrMan> addrman;
3840
std::unique_ptr<CConnman> connman;
3941
std::unique_ptr<CTxMemPool> mempool;
4042
std::unique_ptr<CBlockPolicyEstimator> fee_estimator;

src/test/denialofservice_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
6767
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
6868
{
6969
const CChainParams& chainparams = Params();
70-
auto connman = std::make_unique<CConnman>(0x1337, 0x1337);
70+
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman);
7171
auto peerLogic = PeerManager::make(chainparams, *connman, nullptr, *m_node.scheduler,
7272
*m_node.chainman, *m_node.mempool, false);
7373

@@ -137,7 +137,7 @@ static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerManager &pee
137137
BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
138138
{
139139
const CChainParams& chainparams = Params();
140-
auto connman = std::make_unique<CConnmanTest>(0x1337, 0x1337);
140+
auto connman = std::make_unique<CConnmanTest>(0x1337, 0x1337, *m_node.addrman);
141141
auto peerLogic = PeerManager::make(chainparams, *connman, nullptr, *m_node.scheduler,
142142
*m_node.chainman, *m_node.mempool, false);
143143

@@ -211,7 +211,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
211211
{
212212
const CChainParams& chainparams = Params();
213213
auto banman = std::make_unique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
214-
auto connman = std::make_unique<CConnman>(0x1337, 0x1337);
214+
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman);
215215
auto peerLogic = PeerManager::make(chainparams, *connman, banman.get(), *m_node.scheduler,
216216
*m_node.chainman, *m_node.mempool, false);
217217

@@ -258,7 +258,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
258258
{
259259
const CChainParams& chainparams = Params();
260260
auto banman = std::make_unique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
261-
auto connman = std::make_unique<CConnman>(0x1337, 0x1337);
261+
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman);
262262
auto peerLogic = PeerManager::make(chainparams, *connman, banman.get(), *m_node.scheduler,
263263
*m_node.chainman, *m_node.mempool, false);
264264

src/test/fuzz/connman.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ FUZZ_TARGET_INIT(connman, initialize_connman)
2525
{
2626
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
2727
SetMockTime(ConsumeTime(fuzzed_data_provider));
28-
CConnman connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeBool()};
28+
CAddrMan addrman;
29+
CConnman connman{fuzzed_data_provider.ConsumeIntegral<uint64_t>(), fuzzed_data_provider.ConsumeIntegral<uint64_t>(), addrman, fuzzed_data_provider.ConsumeBool()};
2930
CAddress random_address;
3031
CNetAddr random_netaddr;
3132
CNode random_node = ConsumeNode(fuzzed_data_provider);

src/test/util/setup_common.cpp

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

55
#include <test/util/setup_common.h>
66

7+
#include <addrman.h>
78
#include <banman.h>
89
#include <chainparams.h>
910
#include <consensus/consensus.h>
@@ -155,6 +156,7 @@ ChainTestingSetup::~ChainTestingSetup()
155156
GetMainSignals().UnregisterBackgroundSignalScheduler();
156157
m_node.connman.reset();
157158
m_node.banman.reset();
159+
m_node.addrman.reset();
158160
m_node.args = nullptr;
159161
UnloadBlockIndex(m_node.mempool.get(), *m_node.chainman);
160162
m_node.mempool.reset();
@@ -187,8 +189,9 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
187189
throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", state.ToString()));
188190
}
189191

192+
m_node.addrman = std::make_unique<CAddrMan>();
190193
m_node.banman = std::make_unique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
191-
m_node.connman = std::make_unique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
194+
m_node.connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman); // Deterministic randomness for tests.
192195
m_node.peerman = PeerManager::make(chainparams, *m_node.connman, m_node.banman.get(),
193196
*m_node.scheduler, *m_node.chainman, *m_node.mempool,
194197
false);

0 commit comments

Comments
 (0)