Skip to content

Commit 574682d

Browse files
committed
partial bitcoin#20228: Make addrman a top-level component
1 parent 1659080 commit 574682d

File tree

11 files changed

+86
-119
lines changed

11 files changed

+86
-119
lines changed

src/init.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ void PrepareShutdown(NodeContext& node)
278278
node.peer_logic.reset();
279279
node.connman.reset();
280280
node.banman.reset();
281+
node.addrman.reset();
281282

282283
if (node.mempool && node.mempool->IsLoaded() && node.args->GetArg("-persistmempool", DEFAULT_PERSIST_MEMPOOL)) {
283284
DumpMempool(*node.mempool);
@@ -1755,10 +1756,12 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
17551756
fDiscover = args.GetBoolArg("-discover", true);
17561757
g_relay_txes = !args.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY);
17571758

1759+
assert(!node.addrman);
1760+
node.addrman = std::make_unique<CAddrMan>();
17581761
assert(!node.banman);
17591762
node.banman = std::make_unique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, args.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
17601763
assert(!node.connman);
1761-
node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()));
1764+
node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), *node.addrman);
17621765

17631766
assert(!node.fee_estimator);
17641767
// Don't initialize fee estimation with old data if we don't relay transactions,
@@ -1774,7 +1777,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
17741777
ChainstateManager& chainman = *Assert(node.chainman);
17751778

17761779
node.peer_logic.reset(new PeerLogicValidation(
1777-
*node.connman, node.banman.get(), *node.scheduler, chainman, *node.mempool, node.llmq_ctx
1780+
*node.connman, *node.addrman, node.banman.get(), *node.scheduler, chainman, *node.mempool, node.llmq_ctx
17781781
));
17791782
RegisterValidationInterface(node.peer_logic.get());
17801783

src/net.cpp

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,9 +2949,8 @@ void CConnman::SetNetworkActive(bool active)
29492949
uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
29502950
}
29512951

2952-
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) :
2953-
addrman(Params().AllowMultiplePorts()),
2954-
nSeed0(nSeed0In), nSeed1(nSeed1In)
2952+
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, CAddrMan& addrman_in) :
2953+
addrman(addrman_in), nSeed0(nSeed0In), nSeed1(nSeed1In)
29552954
{
29562955
SetTryNewOutboundPeer(false);
29572956

@@ -3309,11 +3308,7 @@ void CConnman::Stop()
33093308
void CConnman::DeleteNode(CNode* pnode)
33103309
{
33113310
assert(pnode);
3312-
bool fUpdateConnectionTime = false;
3313-
m_msgproc->FinalizeNode(*pnode, fUpdateConnectionTime);
3314-
if(fUpdateConnectionTime) {
3315-
addrman.Connected(pnode->addr);
3316-
}
3311+
m_msgproc->FinalizeNode(*pnode);
33173312
delete pnode;
33183313
}
33193314

@@ -3323,21 +3318,6 @@ CConnman::~CConnman()
33233318
Stop();
33243319
}
33253320

3326-
void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
3327-
{
3328-
addrman.SetServices(addr, nServices);
3329-
}
3330-
3331-
void CConnman::MarkAddressGood(const CAddress& addr)
3332-
{
3333-
addrman.Good(addr);
3334-
}
3335-
3336-
void CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
3337-
{
3338-
addrman.Add(vAddr, addrFrom, nTimePenalty);
3339-
}
3340-
33413321
std::vector<CAddress> CConnman::GetAddresses()
33423322
{
33433323
return addrman.GetAddr();

src/net.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ friend class CNode;
213213
socketEventsMode = connOptions.socketEventsMode;
214214
}
215215

216-
CConnman(uint64_t seed0, uint64_t seed1);
216+
CConnman(uint64_t seed0, uint64_t seed1, CAddrMan& addrman);
217217
~CConnman();
218218
bool Start(CScheduler& scheduler, const Options& options);
219219

@@ -399,9 +399,6 @@ friend class CNode;
399399
void RelayInvFiltered(CInv &inv, const uint256 &relatedTxHash, const int minProtoVersion = MIN_PEER_PROTO_VERSION);
400400

401401
// Addrman functions
402-
void SetServices(const CService &addr, ServiceFlags nServices);
403-
void MarkAddressGood(const CAddress& addr);
404-
void AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty = 0);
405402
std::vector<CAddress> GetAddresses();
406403

407404
// This allows temporarily exceeding m_max_outbound_full_relay, with the goal of finding
@@ -580,7 +577,7 @@ friend class CNode;
580577
std::vector<ListenSocket> vhListenSocket;
581578
std::atomic<bool> fNetworkActive{true};
582579
bool fAddressesInitialized{false};
583-
CAddrMan addrman;
580+
CAddrMan& addrman;
584581
std::deque<std::string> vOneShots GUARDED_BY(cs_vOneShots);
585582
CCriticalSection cs_vOneShots;
586583
std::vector<std::string> vAddedNodes GUARDED_BY(cs_vAddedNodes);
@@ -708,7 +705,7 @@ class NetEventsInterface
708705
virtual bool ProcessMessages(CNode* pnode, std::atomic<bool>& interrupt) = 0;
709706
virtual bool SendMessages(CNode* pnode) = 0;
710707
virtual void InitializeNode(CNode* pnode) = 0;
711-
virtual void FinalizeNode(const CNode& node, bool& update_connection_time) = 0;
708+
virtual void FinalizeNode(const CNode& node) = 0;
712709

713710
protected:
714711
/**

src/net_processing.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -965,11 +965,11 @@ void PeerLogicValidation::ReattemptInitialBroadcast(CScheduler& scheduler) const
965965
scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
966966
}
967967

968-
void PeerLogicValidation::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
968+
void PeerLogicValidation::FinalizeNode(const CNode& node) {
969969
NodeId nodeid = node.GetId();
970-
fUpdateConnectionTime = false;
971-
LOCK(cs_main);
972970
int misbehavior{0};
971+
LOCK(cs_main);
972+
{
973973
{
974974
PeerRef peer = GetPeerRef(nodeid);
975975
assert(peer != nullptr);
@@ -983,11 +983,6 @@ void PeerLogicValidation::FinalizeNode(const CNode& node, bool& fUpdateConnectio
983983
if (state->fSyncStarted)
984984
nSyncStarted--;
985985

986-
if (node.fSuccessfullyConnected && misbehavior == 0 /* && !node.IsBlockOnlyConn() */) {
987-
// Note: we avoid changing visible addrman state for block-relay-only peers
988-
fUpdateConnectionTime = true;
989-
}
990-
991986
for (const QueuedBlock& entry : state->vBlocksInFlight) {
992987
mapBlocksInFlight.erase(entry.hash);
993988
}
@@ -1007,6 +1002,15 @@ void PeerLogicValidation::FinalizeNode(const CNode& node, bool& fUpdateConnectio
10071002
assert(nPeersWithValidatedDownloads == 0);
10081003
assert(g_outbound_peers_with_protect_from_disconnect == 0);
10091004
}
1005+
} // cs_main
1006+
1007+
if (node.fSuccessfullyConnected && misbehavior == 0 /* && !node.IsBlockOnlyConn() */) {
1008+
// Only change visible addrman state for full outbound peers. We don't
1009+
// call Connected() for feeler connections since they don't have
1010+
// fSuccessfullyConnected set.
1011+
m_addrman.Connected(node.addr);
1012+
}
1013+
10101014
LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
10111015
}
10121016

@@ -1296,9 +1300,10 @@ static bool BlockRequestAllowed(const CBlockIndex* pindex, const Consensus::Para
12961300
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, consensusParams) < STALE_RELAY_AGE_LIMIT);
12971301
}
12981302

1299-
PeerLogicValidation::PeerLogicValidation(CConnman& connman, BanMan* banman, CScheduler &scheduler, ChainstateManager& chainman, CTxMemPool& pool,
1303+
PeerLogicValidation::PeerLogicValidation(CConnman& connman, CAddrMan& addrman, BanMan* banman, CScheduler &scheduler, ChainstateManager& chainman, CTxMemPool& pool,
13001304
std::unique_ptr<LLMQContext>& llmq_ctx)
13011305
: m_connman(connman),
1306+
m_addrman(addrman),
13021307
m_banman(banman),
13031308
m_chainman(chainman),
13041309
m_mempool(pool),
@@ -2644,7 +2649,7 @@ void PeerLogicValidation::ProcessMessage(
26442649
nServices = ServiceFlags(nServiceInt);
26452650
if (!pfrom.fInbound)
26462651
{
2647-
m_connman.SetServices(pfrom.addr, nServices);
2652+
m_addrman.SetServices(pfrom.addr, nServices);
26482653
}
26492654
if (!pfrom.fInbound && !pfrom.fFeeler && !pfrom.m_manual_connection && !HasAllDesirableServiceFlags(nServices))
26502655
{
@@ -2782,7 +2787,7 @@ void PeerLogicValidation::ProcessMessage(
27822787
// Get recent addresses
27832788
m_connman.PushMessage(&pfrom, CNetMsgMaker(nSendVersion).Make(NetMsgType::GETADDR));
27842789
pfrom.fGetAddr = true;
2785-
m_connman.MarkAddressGood(pfrom.addr);
2790+
m_addrman.Good(pfrom.addr);
27862791
}
27872792

27882793
std::string remoteAddr;
@@ -2954,7 +2959,7 @@ void PeerLogicValidation::ProcessMessage(
29542959
if (fReachable)
29552960
vAddrOk.push_back(addr);
29562961
}
2957-
m_connman.AddNewAddresses(vAddrOk, pfrom.addr, 2 * 60 * 60);
2962+
m_addrman.Add(vAddrOk, pfrom.addr, 2 * 60 * 60);
29582963
if (vAddr.size() < 1000)
29592964
pfrom.fGetAddr = false;
29602965
if (pfrom.fOneShot)

src/net_processing.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <sync.h>
1212
#include <validationinterface.h>
1313

14+
class CAddrMan;
1415
class CTxMemPool;
1516
class ChainstateManager;
1617
struct LLMQContext;
@@ -28,14 +29,15 @@ class PeerLogicValidation final : public CValidationInterface, public NetEventsI
2829
private:
2930
CConnman& m_connman;
3031
BanMan* const m_banman;
32+
CAddrMan& m_addrman;
3133
ChainstateManager& m_chainman;
3234
CTxMemPool& m_mempool;
3335
std::unique_ptr<LLMQContext>& m_llmq_ctx;
3436

3537
bool MaybeDiscourageAndDisconnect(CNode& pnode);
3638

3739
public:
38-
PeerLogicValidation(CConnman& connman, BanMan* banman, CScheduler &scheduler, ChainstateManager& chainman, CTxMemPool& pool,
40+
PeerLogicValidation(CConnman& connman, CAddrMan& addrman, BanMan* banman, CScheduler &scheduler, ChainstateManager& chainman, CTxMemPool& pool,
3941
std::unique_ptr<LLMQContext>& llmq_ctx);
4042

4143
/**
@@ -59,7 +61,7 @@ class PeerLogicValidation final : public CValidationInterface, public NetEventsI
5961
/** Initialize a peer by adding it to mapNodeState and pushing a message requesting its version */
6062
void InitializeNode(CNode* pnode) override;
6163
/** Handle removal of a peer by updating various state and removing it from mapNodeState */
62-
void FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) override;
64+
void FinalizeNode(const CNode& node) override;
6365
/**
6466
* Process protocol messages received from a given node
6567
*

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 <llmq/context.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;
@@ -37,6 +38,7 @@ class WalletClient;
3738
//! any member functions. It should just be a collection of references that can
3839
//! be used without pulling in unwanted dependencies or functionality.
3940
struct NodeContext {
41+
std::unique_ptr<CAddrMan> addrman;
4042
std::unique_ptr<CConnman> connman;
4143
std::unique_ptr<CTxMemPool> mempool;
4244
std::unique_ptr<CBlockPolicyEstimator> fee_estimator;

src/test/denialofservice_tests.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
8080
// work.
8181
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
8282
{
83-
auto connman = std::make_unique<CConnman>(0x1337, 0x1337);
83+
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman);
8484
auto peerLogic = std::make_unique<PeerLogicValidation>(
85-
*connman, nullptr, *m_node.scheduler, *m_node.chainman, *m_node.mempool, m_node.llmq_ctx
85+
*connman, *m_node.addrman, nullptr, *m_node.scheduler, *m_node.chainman, *m_node.mempool, m_node.llmq_ctx
8686
);
8787

8888
// Mock an outbound peer
@@ -133,8 +133,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
133133
BOOST_CHECK(dummyNode1.fDisconnect == true);
134134
SetMockTime(0);
135135

136-
bool dummy;
137-
peerLogic->FinalizeNode(dummyNode1, dummy);
136+
peerLogic->FinalizeNode(dummyNode1);
138137
}
139138

140139
static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic, CConnmanTest* connman)
@@ -153,9 +152,9 @@ static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidat
153152

154153
BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
155154
{
156-
auto connman = std::make_unique<CConnmanTest>(0x1337, 0x1337);
155+
auto connman = std::make_unique<CConnmanTest>(0x1337, 0x1337, *m_node.addrman);
157156
auto peerLogic = std::make_unique<PeerLogicValidation>(
158-
*connman, nullptr, *m_node.scheduler, *m_node.chainman, *m_node.mempool, m_node.llmq_ctx
157+
*connman, *m_node.addrman, nullptr, *m_node.scheduler, *m_node.chainman, *m_node.mempool, m_node.llmq_ctx
159158
);
160159

161160
const Consensus::Params& consensusParams = Params().GetConsensus();
@@ -217,9 +216,8 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
217216
BOOST_CHECK(vNodes[max_outbound_full_relay-1]->fDisconnect == true);
218217
BOOST_CHECK(vNodes.back()->fDisconnect == false);
219218

220-
bool dummy;
221219
for (const CNode *node : vNodes) {
222-
peerLogic->FinalizeNode(*node, dummy);
220+
peerLogic->FinalizeNode(*node);
223221
}
224222

225223
connman->ClearNodes();
@@ -228,9 +226,9 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
228226
BOOST_AUTO_TEST_CASE(DoS_banning)
229227
{
230228
auto banman = std::make_unique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
231-
auto connman = std::make_unique<CConnman>(0x1337, 0x1337);
229+
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman);
232230
auto peerLogic = std::make_unique<PeerLogicValidation>(
233-
*connman, banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool, m_node.llmq_ctx
231+
*connman, *m_node.addrman, banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool, m_node.llmq_ctx
234232
);
235233

236234
banman->ClearBanned();
@@ -268,17 +266,16 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
268266
}
269267
BOOST_CHECK(banman->IsDiscouraged(addr2));
270268

271-
bool dummy;
272-
peerLogic->FinalizeNode(dummyNode1, dummy);
273-
peerLogic->FinalizeNode(dummyNode2, dummy);
269+
peerLogic->FinalizeNode(dummyNode1);
270+
peerLogic->FinalizeNode(dummyNode2);
274271
}
275272

276273
BOOST_AUTO_TEST_CASE(DoS_banscore)
277274
{
278275
auto banman = std::make_unique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
279-
auto connman = std::make_unique<CConnman>(0x1337, 0x1337);
276+
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman);
280277
auto peerLogic = std::make_unique<PeerLogicValidation>(
281-
*connman, banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool, m_node.llmq_ctx
278+
*connman, *m_node.addrman, banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool, m_node.llmq_ctx
282279
);
283280

284281
banman->ClearBanned();
@@ -315,16 +312,15 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
315312
BOOST_CHECK(banman->IsDiscouraged(addr1));
316313
gArgs.ForceSetArg("-banscore", ToString(DEFAULT_BANSCORE_THRESHOLD));
317314

318-
bool dummy;
319-
peerLogic->FinalizeNode(dummyNode1, dummy);
315+
peerLogic->FinalizeNode(dummyNode1);
320316
}
321317

322318
BOOST_AUTO_TEST_CASE(DoS_bantime)
323319
{
324320
auto banman = std::make_unique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
325-
auto connman = std::make_unique<CConnman>(0x1337, 0x1337);
321+
auto connman = std::make_unique<CConnman>(0x1337, 0x1337, *m_node.addrman);
326322
auto peerLogic = std::make_unique<PeerLogicValidation>(
327-
*connman, banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool, m_node.llmq_ctx
323+
*connman, *m_node.addrman, banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool, m_node.llmq_ctx
328324
);
329325

330326
banman->ClearBanned();
@@ -345,8 +341,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
345341
}
346342
BOOST_CHECK(banman->IsDiscouraged(addr));
347343

348-
bool dummy;
349-
peerLogic->FinalizeNode(dummyNode, dummy);
344+
peerLogic->FinalizeNode(dummyNode);
350345
}
351346

352347
static CTransactionRef RandomOrphan()

0 commit comments

Comments
 (0)