Skip to content

Commit 1999baa

Browse files
author
MarcoFalke
committed
Merge #20228: addrman: Make addrman a top-level component
3fc06d3 [net] remove fUpdateConnectionTime from FinalizeNode (John Newbery) 7c4cc67 [net] remove CConnman::AddNewAddresses (John Newbery) bcd7f30 [net] remove CConnman::MarkAddressGood (John Newbery) 8073673 [net] remove CConnman::SetServices (John Newbery) 392a95d [net_processing] Keep addrman reference in PeerManager (John Newbery) 1c25adf [net] Construct addrman outside connman (John Newbery) Pull request description: Addrman is currently a member variable of connman. Make it a top-level component with lifetime owned by node.context, and add a reference to addrman in peerman. This allows us to eliminate some functions in connman that are simply forwarding requests to addrman, and simplifies the connman-peerman interface. By constructing the addrman in init, we can also add parameters to the ctor, which allows us to test it better. See #20233, where we enable consistency checking for addrman in our functional tests. ACKs for top commit: MarcoFalke: re-ACK 3fc06d3 only change is squash 🏀 vasild: ACK 3fc06d3 Tree-SHA512: 17662c65cbedcd9bd1c194914bc4bb4216f4e3581a06222de78f026d6796f1da6fe3e0bf28c2d26a102a12ad4fbf13f815944a297f000e3acf46faea42855e07
2 parents 3ececa7 + 3fc06d3 commit 1999baa

File tree

11 files changed

+74
-107
lines changed

11 files changed

+74
-107
lines changed

src/init.cpp

Lines changed: 5 additions & 2 deletions
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,
@@ -1421,7 +1424,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
14211424
ChainstateManager& chainman = *Assert(node.chainman);
14221425

14231426
assert(!node.peerman);
1424-
node.peerman = PeerManager::make(chainparams, *node.connman, node.banman.get(),
1427+
node.peerman = PeerManager::make(chainparams, *node.connman, *node.addrman, node.banman.get(),
14251428
*node.scheduler, chainman, *node.mempool, ignores_incoming_txs);
14261429
RegisterValidationInterface(node.peerman.get());
14271430

src/net.cpp

Lines changed: 3 additions & 22 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

@@ -2621,11 +2621,7 @@ void CConnman::StopNodes()
26212621
void CConnman::DeleteNode(CNode* pnode)
26222622
{
26232623
assert(pnode);
2624-
bool fUpdateConnectionTime = false;
2625-
m_msgproc->FinalizeNode(*pnode, fUpdateConnectionTime);
2626-
if (fUpdateConnectionTime) {
2627-
addrman.Connected(pnode->addr);
2628-
}
2624+
m_msgproc->FinalizeNode(*pnode);
26292625
delete pnode;
26302626
}
26312627

@@ -2635,21 +2631,6 @@ CConnman::~CConnman()
26352631
Stop();
26362632
}
26372633

2638-
void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
2639-
{
2640-
addrman.SetServices(addr, nServices);
2641-
}
2642-
2643-
void CConnman::MarkAddressGood(const CAddress& addr)
2644-
{
2645-
addrman.Good(addr);
2646-
}
2647-
2648-
bool CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
2649-
{
2650-
return addrman.Add(vAddr, addrFrom, nTimePenalty);
2651-
}
2652-
26532634
std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct)
26542635
{
26552636
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct);

src/net.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ class NetEventsInterface
770770
virtual void InitializeNode(CNode* pnode) = 0;
771771

772772
/** Handle removal of a peer (clear state) */
773-
virtual void FinalizeNode(const CNode& node, bool& update_connection_time) = 0;
773+
virtual void FinalizeNode(const CNode& node) = 0;
774774

775775
/**
776776
* Process protocol messages received from a given node
@@ -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

@@ -921,9 +921,6 @@ class CConnman
921921
};
922922

923923
// Addrman functions
924-
void SetServices(const CService &addr, ServiceFlags nServices);
925-
void MarkAddressGood(const CAddress& addr);
926-
bool AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty = 0);
927924
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct);
928925
/**
929926
* Cache is used to minimize topology leaks, so it should
@@ -1130,7 +1127,7 @@ class CConnman
11301127
std::vector<ListenSocket> vhListenSocket;
11311128
std::atomic<bool> fNetworkActive{true};
11321129
bool fAddressesInitialized{false};
1133-
CAddrMan addrman;
1130+
CAddrMan& addrman;
11341131
std::deque<std::string> m_addr_fetches GUARDED_BY(m_addr_fetches_mutex);
11351132
RecursiveMutex m_addr_fetches_mutex;
11361133
std::vector<std::string> vAddedNodes GUARDED_BY(cs_vAddedNodes);

src/net_processing.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ using PeerRef = std::shared_ptr<Peer>;
225225
class PeerManagerImpl final : public PeerManager
226226
{
227227
public:
228-
PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
229-
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
230-
bool ignore_incoming_txs);
228+
PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman,
229+
BanMan* banman, CScheduler& scheduler, ChainstateManager& chainman,
230+
CTxMemPool& pool, bool ignore_incoming_txs);
231231

232232
/** Overridden from CValidationInterface. */
233233
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override;
@@ -238,7 +238,7 @@ class PeerManagerImpl final : public PeerManager
238238

239239
/** Implement NetEventsInterface */
240240
void InitializeNode(CNode* pnode) override;
241-
void FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) override;
241+
void FinalizeNode(const CNode& node) override;
242242
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
243243
bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing);
244244

@@ -322,6 +322,7 @@ class PeerManagerImpl final : public PeerManager
322322

323323
const CChainParams& m_chainparams;
324324
CConnman& m_connman;
325+
CAddrMan& m_addrman;
325326
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
326327
BanMan* const m_banman;
327328
ChainstateManager& m_chainman;
@@ -968,12 +969,12 @@ void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
968969
scheduler.scheduleFromNow([&] { ReattemptInitialBroadcast(scheduler); }, delta);
969970
}
970971

971-
void PeerManagerImpl::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime)
972+
void PeerManagerImpl::FinalizeNode(const CNode& node)
972973
{
973974
NodeId nodeid = node.GetId();
974-
fUpdateConnectionTime = false;
975-
LOCK(cs_main);
976975
int misbehavior{0};
976+
{
977+
LOCK(cs_main);
977978
{
978979
// We remove the PeerRef from g_peer_map here, but we don't always
979980
// destruct the Peer. Sometimes another thread is still holding a
@@ -990,12 +991,6 @@ void PeerManagerImpl::FinalizeNode(const CNode& node, bool& fUpdateConnectionTim
990991
if (state->fSyncStarted)
991992
nSyncStarted--;
992993

993-
if (node.fSuccessfullyConnected && misbehavior == 0 &&
994-
!node.IsBlockOnlyConn() && !node.IsInboundConn()) {
995-
// Only change visible addrman state for outbound, full-relay peers
996-
fUpdateConnectionTime = true;
997-
}
998-
999994
for (const QueuedBlock& entry : state->vBlocksInFlight) {
1000995
mapBlocksInFlight.erase(entry.hash);
1001996
}
@@ -1020,6 +1015,14 @@ void PeerManagerImpl::FinalizeNode(const CNode& node, bool& fUpdateConnectionTim
10201015
assert(m_wtxid_relay_peers == 0);
10211016
assert(m_txrequest.Size() == 0);
10221017
}
1018+
} // cs_main
1019+
if (node.fSuccessfullyConnected && misbehavior == 0 &&
1020+
!node.IsBlockOnlyConn() && !node.IsInboundConn()) {
1021+
// Only change visible addrman state for full outbound peers. We don't
1022+
// call Connected() for feeler connections since they don't have
1023+
// fSuccessfullyConnected set.
1024+
m_addrman.Connected(node.addr);
1025+
}
10231026
LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
10241027
}
10251028

@@ -1201,18 +1204,19 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
12011204
(GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
12021205
}
12031206

1204-
std::unique_ptr<PeerManager> PeerManager::make(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
1205-
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
1206-
bool ignore_incoming_txs)
1207+
std::unique_ptr<PeerManager> PeerManager::make(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman,
1208+
BanMan* banman, CScheduler& scheduler, ChainstateManager& chainman,
1209+
CTxMemPool& pool, bool ignore_incoming_txs)
12071210
{
1208-
return std::make_unique<PeerManagerImpl>(chainparams, connman, banman, scheduler, chainman, pool, ignore_incoming_txs);
1211+
return std::make_unique<PeerManagerImpl>(chainparams, connman, addrman, banman, scheduler, chainman, pool, ignore_incoming_txs);
12091212
}
12101213

1211-
PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
1212-
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
1213-
bool ignore_incoming_txs)
1214+
PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman,
1215+
BanMan* banman, CScheduler& scheduler, ChainstateManager& chainman,
1216+
CTxMemPool& pool, bool ignore_incoming_txs)
12141217
: m_chainparams(chainparams),
12151218
m_connman(connman),
1219+
m_addrman(addrman),
12161220
m_banman(banman),
12171221
m_chainman(chainman),
12181222
m_mempool(pool),
@@ -2330,7 +2334,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
23302334
nServices = ServiceFlags(nServiceInt);
23312335
if (!pfrom.IsInboundConn())
23322336
{
2333-
m_connman.SetServices(pfrom.addr, nServices);
2337+
m_addrman.SetServices(pfrom.addr, nServices);
23342338
}
23352339
if (pfrom.ExpectServicesFromConn() && !HasAllDesirableServiceFlags(nServices))
23362340
{
@@ -2474,7 +2478,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
24742478
//
24752479
// This moves an address from New to Tried table in Addrman,
24762480
// resolves tried-table collisions, etc.
2477-
m_connman.MarkAddressGood(pfrom.addr);
2481+
m_addrman.Good(pfrom.addr);
24782482
}
24792483

24802484
std::string remoteAddr;
@@ -2679,7 +2683,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
26792683
if (fReachable)
26802684
vAddrOk.push_back(addr);
26812685
}
2682-
m_connman.AddNewAddresses(vAddrOk, pfrom.addr, 2 * 60 * 60);
2686+
m_addrman.Add(vAddrOk, pfrom.addr, 2 * 60 * 60);
26832687
if (vAddr.size() < 1000)
26842688
pfrom.fGetAddr = false;
26852689
if (pfrom.IsAddrFetchConn()) {

src/net_processing.h

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

13+
class CAddrMan;
1314
class CChainParams;
1415
class CTxMemPool;
1516
class ChainstateManager;
@@ -36,9 +37,9 @@ struct CNodeStateStats {
3637
class PeerManager : public CValidationInterface, public NetEventsInterface
3738
{
3839
public:
39-
static std::unique_ptr<PeerManager> make(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
40-
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
41-
bool ignore_incoming_txs);
40+
static std::unique_ptr<PeerManager> make(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman,
41+
BanMan* banman, CScheduler& scheduler, ChainstateManager& chainman,
42+
CTxMemPool& pool, bool ignore_incoming_txs);
4243
virtual ~PeerManager() { }
4344

4445
/** Get statistics from node state */

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/rpc/net.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -907,8 +907,8 @@ static RPCHelpMan addpeeraddress()
907907
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
908908
{
909909
NodeContext& node = EnsureNodeContext(request.context);
910-
if (!node.connman) {
911-
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
910+
if (!node.addrman) {
911+
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
912912
}
913913

914914
UniValue obj(UniValue::VOBJ);
@@ -925,7 +925,7 @@ static RPCHelpMan addpeeraddress()
925925
address.nTime = GetAdjustedTime();
926926
// The source address is set equal to the address. This is equivalent to the peer
927927
// announcing itself.
928-
if (!node.connman->AddNewAddresses({address}, address)) {
928+
if (!node.addrman->Add(address, address)) {
929929
obj.pushKV("success", false);
930930
return obj;
931931
}

0 commit comments

Comments
 (0)