Skip to content

Commit

Permalink
Merge bitcoin#20228: addrman: Make addrman a top-level component
Browse files Browse the repository at this point in the history
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 bitcoin#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
  • Loading branch information
MarcoFalke committed Mar 30, 2021
2 parents 3ececa7 + 3fc06d3 commit 1999baa
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 107 deletions.
7 changes: 5 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ void Shutdown(NodeContext& node)
node.peerman.reset();
node.connman.reset();
node.banman.reset();
node.addrman.reset();

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

assert(!node.addrman);
node.addrman = std::make_unique<CAddrMan>();
assert(!node.banman);
node.banman = std::make_unique<BanMan>(GetDataDir() / "banlist.dat", &uiInterface, args.GetArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
assert(!node.connman);
node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()), args.GetBoolArg("-networkactive", true));
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));

assert(!node.fee_estimator);
// Don't initialize fee estimation with old data if we don't relay transactions,
Expand All @@ -1421,7 +1424,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
ChainstateManager& chainman = *Assert(node.chainman);

assert(!node.peerman);
node.peerman = PeerManager::make(chainparams, *node.connman, node.banman.get(),
node.peerman = PeerManager::make(chainparams, *node.connman, *node.addrman, node.banman.get(),
*node.scheduler, chainman, *node.mempool, ignores_incoming_txs);
RegisterValidationInterface(node.peerman.get());

Expand Down
25 changes: 3 additions & 22 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2351,8 +2351,8 @@ void CConnman::SetNetworkActive(bool active)
uiInterface.NotifyNetworkActiveChanged(fNetworkActive);
}

CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, bool network_active)
: nSeed0(nSeed0In), nSeed1(nSeed1In)
CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In, CAddrMan& addrman_in, bool network_active)
: addrman(addrman_in), nSeed0(nSeed0In), nSeed1(nSeed1In)
{
SetTryNewOutboundPeer(false);

Expand Down Expand Up @@ -2621,11 +2621,7 @@ void CConnman::StopNodes()
void CConnman::DeleteNode(CNode* pnode)
{
assert(pnode);
bool fUpdateConnectionTime = false;
m_msgproc->FinalizeNode(*pnode, fUpdateConnectionTime);
if (fUpdateConnectionTime) {
addrman.Connected(pnode->addr);
}
m_msgproc->FinalizeNode(*pnode);
delete pnode;
}

Expand All @@ -2635,21 +2631,6 @@ CConnman::~CConnman()
Stop();
}

void CConnman::SetServices(const CService &addr, ServiceFlags nServices)
{
addrman.SetServices(addr, nServices);
}

void CConnman::MarkAddressGood(const CAddress& addr)
{
addrman.Good(addr);
}

bool CConnman::AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty)
{
return addrman.Add(vAddr, addrFrom, nTimePenalty);
}

std::vector<CAddress> CConnman::GetAddresses(size_t max_addresses, size_t max_pct)
{
std::vector<CAddress> addresses = addrman.GetAddr(max_addresses, max_pct);
Expand Down
9 changes: 3 additions & 6 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ class NetEventsInterface
virtual void InitializeNode(CNode* pnode) = 0;

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

/**
* Process protocol messages received from a given node
Expand Down Expand Up @@ -856,7 +856,7 @@ class CConnman
m_onion_binds = connOptions.onion_binds;
}

CConnman(uint64_t seed0, uint64_t seed1, bool network_active = true);
CConnman(uint64_t seed0, uint64_t seed1, CAddrMan& addrman, bool network_active = true);
~CConnman();
bool Start(CScheduler& scheduler, const Options& options);

Expand Down Expand Up @@ -921,9 +921,6 @@ class CConnman
};

// Addrman functions
void SetServices(const CService &addr, ServiceFlags nServices);
void MarkAddressGood(const CAddress& addr);
bool AddNewAddresses(const std::vector<CAddress>& vAddr, const CAddress& addrFrom, int64_t nTimePenalty = 0);
std::vector<CAddress> GetAddresses(size_t max_addresses, size_t max_pct);
/**
* Cache is used to minimize topology leaks, so it should
Expand Down Expand Up @@ -1130,7 +1127,7 @@ class CConnman
std::vector<ListenSocket> vhListenSocket;
std::atomic<bool> fNetworkActive{true};
bool fAddressesInitialized{false};
CAddrMan addrman;
CAddrMan& addrman;
std::deque<std::string> m_addr_fetches GUARDED_BY(m_addr_fetches_mutex);
RecursiveMutex m_addr_fetches_mutex;
std::vector<std::string> vAddedNodes GUARDED_BY(cs_vAddedNodes);
Expand Down
50 changes: 27 additions & 23 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ using PeerRef = std::shared_ptr<Peer>;
class PeerManagerImpl final : public PeerManager
{
public:
PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
bool ignore_incoming_txs);
PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman,
BanMan* banman, CScheduler& scheduler, ChainstateManager& chainman,
CTxMemPool& pool, bool ignore_incoming_txs);

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

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

Expand Down Expand Up @@ -322,6 +322,7 @@ class PeerManagerImpl final : public PeerManager

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

void PeerManagerImpl::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime)
void PeerManagerImpl::FinalizeNode(const CNode& node)
{
NodeId nodeid = node.GetId();
fUpdateConnectionTime = false;
LOCK(cs_main);
int misbehavior{0};
{
LOCK(cs_main);
{
// We remove the PeerRef from g_peer_map here, but we don't always
// destruct the Peer. Sometimes another thread is still holding a
Expand All @@ -990,12 +991,6 @@ void PeerManagerImpl::FinalizeNode(const CNode& node, bool& fUpdateConnectionTim
if (state->fSyncStarted)
nSyncStarted--;

if (node.fSuccessfullyConnected && misbehavior == 0 &&
!node.IsBlockOnlyConn() && !node.IsInboundConn()) {
// Only change visible addrman state for outbound, full-relay peers
fUpdateConnectionTime = true;
}

for (const QueuedBlock& entry : state->vBlocksInFlight) {
mapBlocksInFlight.erase(entry.hash);
}
Expand All @@ -1020,6 +1015,14 @@ void PeerManagerImpl::FinalizeNode(const CNode& node, bool& fUpdateConnectionTim
assert(m_wtxid_relay_peers == 0);
assert(m_txrequest.Size() == 0);
}
} // cs_main
if (node.fSuccessfullyConnected && misbehavior == 0 &&
!node.IsBlockOnlyConn() && !node.IsInboundConn()) {
// Only change visible addrman state for full outbound peers. We don't
// call Connected() for feeler connections since they don't have
// fSuccessfullyConnected set.
m_addrman.Connected(node.addr);
}
LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
}

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

std::unique_ptr<PeerManager> PeerManager::make(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
bool ignore_incoming_txs)
std::unique_ptr<PeerManager> PeerManager::make(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman,
BanMan* banman, CScheduler& scheduler, ChainstateManager& chainman,
CTxMemPool& pool, bool ignore_incoming_txs)
{
return std::make_unique<PeerManagerImpl>(chainparams, connman, banman, scheduler, chainman, pool, ignore_incoming_txs);
return std::make_unique<PeerManagerImpl>(chainparams, connman, addrman, banman, scheduler, chainman, pool, ignore_incoming_txs);
}

PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
bool ignore_incoming_txs)
PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman,
BanMan* banman, CScheduler& scheduler, ChainstateManager& chainman,
CTxMemPool& pool, bool ignore_incoming_txs)
: m_chainparams(chainparams),
m_connman(connman),
m_addrman(addrman),
m_banman(banman),
m_chainman(chainman),
m_mempool(pool),
Expand Down Expand Up @@ -2330,7 +2334,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
nServices = ServiceFlags(nServiceInt);
if (!pfrom.IsInboundConn())
{
m_connman.SetServices(pfrom.addr, nServices);
m_addrman.SetServices(pfrom.addr, nServices);
}
if (pfrom.ExpectServicesFromConn() && !HasAllDesirableServiceFlags(nServices))
{
Expand Down Expand Up @@ -2474,7 +2478,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
//
// This moves an address from New to Tried table in Addrman,
// resolves tried-table collisions, etc.
m_connman.MarkAddressGood(pfrom.addr);
m_addrman.Good(pfrom.addr);
}

std::string remoteAddr;
Expand Down Expand Up @@ -2679,7 +2683,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
if (fReachable)
vAddrOk.push_back(addr);
}
m_connman.AddNewAddresses(vAddrOk, pfrom.addr, 2 * 60 * 60);
m_addrman.Add(vAddrOk, pfrom.addr, 2 * 60 * 60);
if (vAddr.size() < 1000)
pfrom.fGetAddr = false;
if (pfrom.IsAddrFetchConn()) {
Expand Down
7 changes: 4 additions & 3 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <sync.h>
#include <validationinterface.h>

class CAddrMan;
class CChainParams;
class CTxMemPool;
class ChainstateManager;
Expand All @@ -36,9 +37,9 @@ struct CNodeStateStats {
class PeerManager : public CValidationInterface, public NetEventsInterface
{
public:
static std::unique_ptr<PeerManager> make(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
bool ignore_incoming_txs);
static std::unique_ptr<PeerManager> make(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman,
BanMan* banman, CScheduler& scheduler, ChainstateManager& chainman,
CTxMemPool& pool, bool ignore_incoming_txs);
virtual ~PeerManager() { }

/** Get statistics from node state */
Expand Down
1 change: 1 addition & 0 deletions src/node/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <node/context.h>

#include <addrman.h>
#include <banman.h>
#include <interfaces/chain.h>
#include <net.h>
Expand Down
2 changes: 2 additions & 0 deletions src/node/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class ArgsManager;
class BanMan;
class CAddrMan;
class CBlockPolicyEstimator;
class CConnman;
class CScheduler;
Expand All @@ -35,6 +36,7 @@ class WalletClient;
//! any member functions. It should just be a collection of references that can
//! be used without pulling in unwanted dependencies or functionality.
struct NodeContext {
std::unique_ptr<CAddrMan> addrman;
std::unique_ptr<CConnman> connman;
std::unique_ptr<CTxMemPool> mempool;
std::unique_ptr<CBlockPolicyEstimator> fee_estimator;
Expand Down
6 changes: 3 additions & 3 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,8 +907,8 @@ static RPCHelpMan addpeeraddress()
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
NodeContext& node = EnsureNodeContext(request.context);
if (!node.connman) {
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
if (!node.addrman) {
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Address manager functionality missing or disabled");
}

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

0 comments on commit 1999baa

Please sign in to comment.